-
Notifications
You must be signed in to change notification settings - Fork 531
/
AbstractStreamDecoder.java
771 lines (685 loc) · 24.4 KB
/
AbstractStreamDecoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
package org.apache.maven.surefire.api.stream;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.nio.charset.CodingErrorAction.REPLACE;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.util.Arrays.copyOf;
import static org.apache.maven.surefire.api.booter.Constants.DEFAULT_STREAM_ENCODING;
import static org.apache.maven.surefire.api.stream.AbstractStreamDecoder.StreamReadStatus.OVERFLOW;
import static org.apache.maven.surefire.api.stream.AbstractStreamDecoder.StreamReadStatus.UNDERFLOW;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isBlank;
/**
* @param <M> message object
* @param <MT> enum describing the meaning of the message
* @param <ST> enum for segment type
*/
public abstract class AbstractStreamDecoder<M, MT extends Enum<MT>, ST extends Enum<ST>> implements AutoCloseable
{
public static final int BUFFER_SIZE = 1024;
private static final String PRINTABLE_JVM_NATIVE_STREAM = "Listening for transport dt_socket at address:";
private static final String[] JVM_ERROR_PATTERNS = {
"could not create the java virtual machine", "error occurred during initialization", // of VM, of boot layer
"error:", // general errors
"could not reserve enough space", "could not allocate", "unable to allocate", // memory errors
"java.lang.module.findexception" // JPMS errors
};
private static final byte[] DEFAULT_STREAM_ENCODING_BYTES = DEFAULT_STREAM_ENCODING.name().getBytes( US_ASCII );
private static final int NO_POSITION = -1;
private static final int DELIMITER_LENGTH = 1;
private static final int BYTE_LENGTH = 1;
private static final int INT_LENGTH = 4;
private static final int LONG_LENGTH = 8;
private final ReadableByteChannel channel;
private final ForkNodeArguments arguments;
private final Map<Segment, MT> messageTypes;
private final ConsoleLogger logger;
protected AbstractStreamDecoder( @Nonnull ReadableByteChannel channel,
@Nonnull ForkNodeArguments arguments,
@Nonnull Map<Segment, MT> messageTypes )
{
this.channel = channel;
this.arguments = arguments;
this.messageTypes = messageTypes;
logger = arguments.getConsoleLogger();
}
public abstract M decode( @Nonnull Memento memento ) throws MalformedChannelException, IOException;
@Nonnull
protected abstract byte[] getEncodedMagicNumber();
@Nonnull
protected abstract ST[] nextSegmentType( @Nonnull MT messageType );
@Nonnull
protected abstract M toMessage( @Nonnull MT messageType, @Nonnull Memento memento )
throws MalformedFrameException;
@Nonnull
protected final ForkNodeArguments getArguments()
{
return arguments;
}
protected void debugStream( byte[] array, int position, int remaining )
{
}
protected MT readMessageType( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
byte[] header = getEncodedMagicNumber();
int readCount = DELIMITER_LENGTH + header.length + DELIMITER_LENGTH + BYTE_LENGTH + DELIMITER_LENGTH;
read( memento, readCount );
checkHeader( memento );
return messageTypes.get( readSegment( memento ) );
}
@Nonnull
@SuppressWarnings( "checkstyle:magicnumber" )
protected Segment readSegment( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
int readCount = readByte( memento ) & 0xff;
read( memento, readCount + DELIMITER_LENGTH );
ByteBuffer bb = memento.getByteBuffer();
Segment segment = new Segment( bb.array(), bb.arrayOffset() + ( (Buffer) bb ).position(), readCount );
( (Buffer) bb ).position( ( (Buffer) bb ).position() + readCount );
checkDelimiter( memento );
return segment;
}
@Nonnull
@SuppressWarnings( "checkstyle:magicnumber" )
protected Charset readCharset( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
int length = readByte( memento ) & 0xff;
read( memento, length + DELIMITER_LENGTH );
ByteBuffer bb = memento.getByteBuffer();
byte[] array = bb.array();
int offset = bb.arrayOffset() + ( (Buffer) bb ).position();
( (Buffer) bb ).position( ( (Buffer) bb ).position() + length );
boolean isDefaultEncoding = false;
if ( length == DEFAULT_STREAM_ENCODING_BYTES.length )
{
isDefaultEncoding = true;
for ( int i = 0; i < length; i++ )
{
isDefaultEncoding &= DEFAULT_STREAM_ENCODING_BYTES[i] == array[offset + i];
}
}
try
{
Charset charset =
isDefaultEncoding
? DEFAULT_STREAM_ENCODING
: Charset.forName( new String( array, offset, length, US_ASCII ) );
checkDelimiter( memento );
return charset;
}
catch ( IllegalArgumentException e )
{
throw new MalformedFrameException( memento.getLine().getPositionByteBuffer(), ( (Buffer) bb ).position() );
}
}
protected String readString( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
( (Buffer) memento.getCharBuffer() ).clear();
int readCount = readInt( memento );
if ( readCount < 0 )
{
throw new MalformedFrameException( memento.getLine().getPositionByteBuffer(),
( (Buffer) memento.getByteBuffer() ).position() );
}
read( memento, readCount + DELIMITER_LENGTH );
final String string;
if ( readCount == 0 )
{
string = "";
}
else if ( readCount == 1 )
{
read( memento, 1 );
byte oneChar = memento.getByteBuffer().get();
string = oneChar == 0 ? null : String.valueOf( (char) oneChar );
}
else
{
string = readString( memento, readCount );
}
read( memento, 1 );
checkDelimiter( memento );
return string;
}
protected Integer readInteger( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
read( memento, BYTE_LENGTH );
boolean isNullObject = memento.getByteBuffer().get() == 0;
if ( isNullObject )
{
read( memento, DELIMITER_LENGTH );
checkDelimiter( memento );
return null;
}
return readInt( memento );
}
protected byte readByte( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
read( memento, BYTE_LENGTH + DELIMITER_LENGTH );
byte b = memento.getByteBuffer().get();
checkDelimiter( memento );
return b;
}
protected int readInt( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
read( memento, INT_LENGTH + DELIMITER_LENGTH );
int i = memento.getByteBuffer().getInt();
checkDelimiter( memento );
return i;
}
protected Long readLong( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
read( memento, BYTE_LENGTH );
boolean isNullObject = memento.getByteBuffer().get() == 0;
if ( isNullObject )
{
read( memento, DELIMITER_LENGTH );
checkDelimiter( memento );
return null;
}
return readLongPrivate( memento );
}
protected long readLongPrivate( @Nonnull Memento memento ) throws IOException, MalformedFrameException
{
read( memento, LONG_LENGTH + DELIMITER_LENGTH );
long num = memento.getByteBuffer().getLong();
checkDelimiter( memento );
return num;
}
@SuppressWarnings( "checkstyle:magicnumber" )
protected final void checkDelimiter( Memento memento ) throws MalformedFrameException
{
ByteBuffer bb = memento.bb;
if ( ( 0xff & bb.get() ) != ':' )
{
throw new MalformedFrameException( memento.getLine().getPositionByteBuffer(), ( (Buffer) bb ).position() );
}
}
protected final void checkHeader( Memento memento ) throws MalformedFrameException
{
ByteBuffer bb = memento.bb;
checkDelimiter( memento );
int shift = 0;
try
{
byte[] header = getEncodedMagicNumber();
byte[] bbArray = bb.array();
for ( int start = bb.arrayOffset() + ( (Buffer) bb ).position(), length = header.length;
shift < length; shift++ )
{
if ( bbArray[shift + start] != header[shift] )
{
throw new MalformedFrameException( memento.getLine().getPositionByteBuffer(),
( (Buffer) bb ).position() + shift );
}
}
}
finally
{
( (Buffer) bb ).position( ( (Buffer) bb ).position() + shift );
}
checkDelimiter( memento );
}
protected void checkArguments( Memento memento, int expectedDataElements )
throws MalformedFrameException
{
if ( memento.getData().size() != expectedDataElements )
{
throw new MalformedFrameException( memento.getLine().getPositionByteBuffer(),
( (Buffer) memento.getByteBuffer() ).position() );
}
}
private String readString( @Nonnull final Memento memento, @Nonnegative final int totalBytes )
throws IOException, MalformedFrameException
{
memento.getDecoder().reset();
final CharBuffer output = memento.getCharBuffer();
( (Buffer) output ).clear();
final ByteBuffer input = memento.getByteBuffer();
final List<String> strings = new ArrayList<>();
int countDecodedBytes = 0;
for ( boolean endOfInput = false; !endOfInput; )
{
final int bytesToRead = totalBytes - countDecodedBytes;
read( memento, bytesToRead );
int bytesToDecode = min( input.remaining(), bytesToRead );
final boolean isLastChunk = bytesToDecode == bytesToRead;
endOfInput = countDecodedBytes + bytesToDecode >= totalBytes;
do
{
boolean endOfChunk = output.remaining() >= bytesToRead;
boolean endOfOutput = isLastChunk && endOfChunk;
int readInputBytes = decodeString( memento.getDecoder(), input, output, bytesToDecode, endOfOutput,
memento.getLine().getPositionByteBuffer() );
bytesToDecode -= readInputBytes;
countDecodedBytes += readInputBytes;
}
while ( isLastChunk && bytesToDecode > 0 && output.hasRemaining() );
strings.add( ( (Buffer) output ).flip().toString() );
( (Buffer) output ).clear();
}
memento.getDecoder().reset();
( (Buffer) output ).clear();
return toString( strings );
}
private static int decodeString( @Nonnull CharsetDecoder decoder, @Nonnull ByteBuffer input,
@Nonnull CharBuffer output, @Nonnegative int bytesToDecode,
boolean endOfInput, @Nonnegative int errorStreamFrom )
throws MalformedFrameException
{
int limit = ( (Buffer) input ).limit();
( (Buffer) input ).limit( ( (Buffer) input ).position() + bytesToDecode );
CoderResult result = decoder.decode( input, output, endOfInput );
if ( result.isError() || result.isMalformed() )
{
throw new MalformedFrameException( errorStreamFrom, ( (Buffer) input ).position() );
}
int decodedBytes = bytesToDecode - input.remaining();
( (Buffer) input ).limit( limit );
return decodedBytes;
}
private static String toString( List<String> strings )
{
if ( strings.size() == 1 )
{
return strings.get( 0 );
}
StringBuilder concatenated = new StringBuilder( strings.size() * BUFFER_SIZE );
for ( String s : strings )
{
concatenated.append( s );
}
return concatenated.toString();
}
private void printCorruptedStream( Memento memento )
{
ByteBuffer bb = memento.getByteBuffer();
if ( bb.hasRemaining() )
{
int bytesToWrite = bb.remaining();
memento.getLine().write( bb, ( (Buffer) bb ).position(), bytesToWrite );
( (Buffer) bb ).position( ( (Buffer) bb ).position() + bytesToWrite );
}
}
/**
* Print the last string which has not been finished by a new line character.
*
* @param memento current memento object
*/
protected final void printRemainingStream( Memento memento )
{
printCorruptedStream( memento );
memento.getLine().printExistingLine();
memento.getLine().clear();
}
/**
*
*/
public static final class Segment
{
private final byte[] array;
private final int fromIndex;
private final int length;
private final int hashCode;
public Segment( byte[] array, int fromIndex, int length )
{
this.array = array;
this.fromIndex = fromIndex;
this.length = length;
int hashCode = 0;
int i = fromIndex;
for ( int loops = length >> 1; loops-- != 0; )
{
hashCode = 31 * hashCode + array[i++];
hashCode = 31 * hashCode + array[i++];
}
this.hashCode = i == fromIndex + length ? hashCode : 31 * hashCode + array[i];
}
@Override
public int hashCode()
{
return hashCode;
}
@Override
public boolean equals( Object obj )
{
if ( !( obj instanceof Segment ) )
{
return false;
}
Segment that = (Segment) obj;
if ( that.length != length )
{
return false;
}
for ( int i = 0; i < length; i++ )
{
if ( that.array[that.fromIndex + i] != array[fromIndex + i] )
{
return false;
}
}
return true;
}
}
protected @Nonnull StreamReadStatus read( @Nonnull Memento memento, int recommendedCount ) throws IOException
{
ByteBuffer buffer = memento.getByteBuffer();
if ( buffer.remaining() >= recommendedCount && ( (Buffer) buffer ).limit() != 0 )
{
return OVERFLOW;
}
else
{
if ( ( (Buffer) buffer ).position() != 0
&& recommendedCount > buffer.capacity() - ( (Buffer) buffer ).position() )
{
( (Buffer) buffer.compact() ).flip();
memento.getLine().setPositionByteBuffer( 0 );
}
int mark = ( (Buffer) buffer ).position();
( (Buffer) buffer ).position( ( (Buffer) buffer ).limit() );
( (Buffer) buffer ).limit( min( ( (Buffer) buffer ).position() + recommendedCount, buffer.capacity() ) );
return read( buffer, mark, recommendedCount );
}
}
private StreamReadStatus read( ByteBuffer buffer, int oldPosition, int recommendedCount )
throws IOException
{
StreamReadStatus readStatus = null;
boolean isEnd = false;
try
{
while ( !isEnd && ( (Buffer) buffer ).position() - oldPosition < recommendedCount
&& ( (Buffer) buffer ).position() < ( (Buffer) buffer ).limit() )
{
isEnd = channel.read( buffer ) == -1;
}
}
finally
{
( (Buffer) buffer ).limit( ( (Buffer) buffer ).position() );
( (Buffer) buffer ).position( oldPosition );
int readBytes = buffer.remaining();
boolean readComplete = readBytes >= recommendedCount;
if ( !isEnd || readComplete )
{
debugStream( buffer.array(),
buffer.arrayOffset() + ( (Buffer) buffer ).position(), buffer.remaining() );
readStatus = readComplete ? OVERFLOW : UNDERFLOW;
}
}
if ( readStatus == null )
{
throw new EOFException();
}
else
{
return readStatus;
}
}
/**
*
*/
public final class Memento
{
private CharsetDecoder currentDecoder;
private final CharsetDecoder defaultDecoder;
private final BufferedStream line = new BufferedStream( 32 );
private final List<Object> data = new ArrayList<>();
private final CharBuffer cb = CharBuffer.allocate( BUFFER_SIZE );
private final ByteBuffer bb = ByteBuffer.allocate( BUFFER_SIZE );
public Memento()
{
defaultDecoder = DEFAULT_STREAM_ENCODING.newDecoder()
.onMalformedInput( REPLACE )
.onUnmappableCharacter( REPLACE );
( (Buffer) bb ).limit( 0 );
}
public void reset()
{
currentDecoder = null;
data.clear();
}
public CharsetDecoder getDecoder()
{
return currentDecoder == null ? defaultDecoder : currentDecoder;
}
public void setCharset( Charset charset )
{
if ( charset.name().equals( defaultDecoder.charset().name() ) )
{
currentDecoder = defaultDecoder;
}
else
{
currentDecoder = charset.newDecoder()
.onMalformedInput( REPLACE )
.onUnmappableCharacter( REPLACE );
}
}
public BufferedStream getLine()
{
return line;
}
public List<Object> getData()
{
return data;
}
public <T> T ofDataAt( int indexOfData )
{
//noinspection unchecked
return (T) data.get( indexOfData );
}
public CharBuffer getCharBuffer()
{
return cb;
}
public ByteBuffer getByteBuffer()
{
return bb;
}
}
/**
* This class avoids locking which gains the performance of this decoder.
*/
public final class BufferedStream
{
private byte[] buffer;
private int count;
private int positionByteBuffer;
private boolean isNewLine;
BufferedStream( int capacity )
{
this.buffer = new byte[capacity];
}
public int getPositionByteBuffer()
{
return positionByteBuffer;
}
public void setPositionByteBuffer( int positionByteBuffer )
{
this.positionByteBuffer = positionByteBuffer;
}
public void write( ByteBuffer bb, int position, int length )
{
ensureCapacity( length );
byte[] array = bb.array();
int pos = bb.arrayOffset() + position;
while ( length-- > 0 )
{
positionByteBuffer++;
byte b = array[pos++];
if ( b == '\r' || b == '\n' )
{
if ( !isNewLine )
{
printExistingLine();
count = 0;
}
isNewLine = true;
}
else
{
buffer[count++] = b;
isNewLine = false;
}
}
}
public void clear()
{
count = 0;
}
@Override
public String toString()
{
return new String( buffer, 0, count, DEFAULT_STREAM_ENCODING );
}
private boolean isEmpty()
{
return count == 0;
}
private void ensureCapacity( int addCapacity )
{
int oldCapacity = buffer.length;
int exactCapacity = count + addCapacity;
if ( exactCapacity < 0 )
{
throw new OutOfMemoryError();
}
if ( oldCapacity < exactCapacity )
{
int newCapacity = oldCapacity << 1;
buffer = copyOf( buffer, max( newCapacity, exactCapacity ) );
}
}
void printExistingLine()
{
if ( isEmpty() )
{
return;
}
String s = toString();
if ( isBlank( s ) )
{
return;
}
if ( s.contains( PRINTABLE_JVM_NATIVE_STREAM ) )
{
if ( logger.isDebugEnabled() )
{
logger.debug( s );
}
else if ( logger.isInfoEnabled() )
{
logger.info( s );
}
else
{
// In case of debugging forked JVM, see PRINTABLE_JVM_NATIVE_STREAM.
System.out.println( s );
}
}
else
{
if ( isJvmError( s ) )
{
logger.error( s );
}
else if ( logger.isDebugEnabled() )
{
logger.debug( s );
}
String msg = "Corrupted channel by directly writing to native stream in forked JVM "
+ arguments.getForkChannelId() + ".";
File dumpFile = arguments.dumpStreamText( msg + " Stream '" + s + "'." );
String dumpPath = dumpFile.getAbsolutePath();
arguments.logWarningAtEnd( msg + " See FAQ web page and the dump file " + dumpPath );
}
}
private boolean isJvmError( String line )
{
String lineLower = line.toLowerCase();
for ( String errorPattern : JVM_ERROR_PATTERNS )
{
if ( lineLower.contains( errorPattern ) )
{
return true;
}
}
return false;
}
}
/**
*
*/
public static final class MalformedFrameException extends Exception
{
private final int readFrom;
private final int readTo;
public MalformedFrameException( int readFrom, int readTo )
{
this.readFrom = readFrom;
this.readTo = readTo;
}
public int readFrom()
{
return readFrom;
}
public int readTo()
{
return readTo;
}
public boolean hasValidPositions()
{
return readFrom != NO_POSITION && readTo != NO_POSITION && readTo - readFrom > 0;
}
}
/**
* Underflow - could not completely read out al bytes in one call.
* <br>
* Overflow - read all bytes or more
* <br>
* EOF - end of stream
*/
public enum StreamReadStatus
{
UNDERFLOW,
OVERFLOW,
EOF
}
}