@Test public void shouldIgnoreDamagedStream() throws IOException { assertThat( BYE_ACK ).isSameAs( Command.BYE_ACK.getCommandType() ); assertEquals( Void.class, BYE_ACK.getDataType() ); byte[] encoded = ":maven-surefire-command:bye-ack:".getBytes(); byte[] streamContent = ( "<something>" + new String( encoded ) + "<damaged>" ).getBytes(); ByteArrayInputStream is = new ByteArrayInputStream( streamContent ); LegacyMasterProcessChannelDecoder decoder = new LegacyMasterProcessChannelDecoder( newChannel( is ) ); Command command = decoder.decode(); assertThat( command.getCommandType() ).isSameAs( BYE_ACK ); assertNull( command.getData() ); }
@Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) public Command decode() throws IOException { List<String> tokens = new ArrayList<>( 3 ); StringBuilder token = new StringBuilder( MAGIC_NUMBER.length() ); ByteBuffer buffer = ByteBuffer.allocate( 1 ); start: do { boolean endOfStream; tokens.clear(); token.setLength( 0 ); FrameCompletion completion = null; for ( boolean frameStarted = false; !( endOfStream = channel.read( buffer ) == -1 ); completion = null ) { buffer.flip(); char c = (char) buffer.get(); buffer.clear(); if ( !frameStarted ) { if ( c == ':' ) { frameStarted = true; token.setLength( 0 ); tokens.clear(); } } else { if ( c == ':' ) { tokens.add( token.toString() ); token.setLength( 0 ); completion = frameCompleteness( tokens ); if ( completion == FrameCompletion.COMPLETE ) { break; } else if ( completion == FrameCompletion.MALFORMED ) { DumpErrorSingleton.getSingleton() .dumpStreamText( "Malformed frame with tokens " + tokens ); continue start; } } else { token.append( c ); } } } if ( completion == FrameCompletion.COMPLETE ) { MasterProcessCommand cmd = COMMAND_OPCODES.get( tokens.get( 1 ) ); if ( tokens.size() == 2 ) { return new Command( cmd ); } else if ( tokens.size() == 3 ) { return new Command( cmd, tokens.get( 2 ) ); } } if ( endOfStream ) { throw new EOFException(); } } while ( true ); }
LegacyMasterProcessChannelDecoder implements MasterProcessChannelDecoder { @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) public Command decode() throws IOException { List<String> tokens = new ArrayList<>( 3 ); StringBuilder token = new StringBuilder( MAGIC_NUMBER.length() ); ByteBuffer buffer = ByteBuffer.allocate( 1 ); start: do { boolean endOfStream; tokens.clear(); token.setLength( 0 ); FrameCompletion completion = null; for ( boolean frameStarted = false; !( endOfStream = channel.read( buffer ) == -1 ); completion = null ) { buffer.flip(); char c = (char) buffer.get(); buffer.clear(); if ( !frameStarted ) { if ( c == ':' ) { frameStarted = true; token.setLength( 0 ); tokens.clear(); } } else { if ( c == ':' ) { tokens.add( token.toString() ); token.setLength( 0 ); completion = frameCompleteness( tokens ); if ( completion == FrameCompletion.COMPLETE ) { break; } else if ( completion == FrameCompletion.MALFORMED ) { DumpErrorSingleton.getSingleton() .dumpStreamText( "Malformed frame with tokens " + tokens ); continue start; } } else { token.append( c ); } } } if ( completion == FrameCompletion.COMPLETE ) { MasterProcessCommand cmd = COMMAND_OPCODES.get( tokens.get( 1 ) ); if ( tokens.size() == 2 ) { return new Command( cmd ); } else if ( tokens.size() == 3 ) { return new Command( cmd, tokens.get( 2 ) ); } } if ( endOfStream ) { throw new EOFException(); } } while ( true ); } }
LegacyMasterProcessChannelDecoder implements MasterProcessChannelDecoder { @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) public Command decode() throws IOException { List<String> tokens = new ArrayList<>( 3 ); StringBuilder token = new StringBuilder( MAGIC_NUMBER.length() ); ByteBuffer buffer = ByteBuffer.allocate( 1 ); start: do { boolean endOfStream; tokens.clear(); token.setLength( 0 ); FrameCompletion completion = null; for ( boolean frameStarted = false; !( endOfStream = channel.read( buffer ) == -1 ); completion = null ) { buffer.flip(); char c = (char) buffer.get(); buffer.clear(); if ( !frameStarted ) { if ( c == ':' ) { frameStarted = true; token.setLength( 0 ); tokens.clear(); } } else { if ( c == ':' ) { tokens.add( token.toString() ); token.setLength( 0 ); completion = frameCompleteness( tokens ); if ( completion == FrameCompletion.COMPLETE ) { break; } else if ( completion == FrameCompletion.MALFORMED ) { DumpErrorSingleton.getSingleton() .dumpStreamText( "Malformed frame with tokens " + tokens ); continue start; } } else { token.append( c ); } } } if ( completion == FrameCompletion.COMPLETE ) { MasterProcessCommand cmd = COMMAND_OPCODES.get( tokens.get( 1 ) ); if ( tokens.size() == 2 ) { return new Command( cmd ); } else if ( tokens.size() == 3 ) { return new Command( cmd, tokens.get( 2 ) ); } } if ( endOfStream ) { throw new EOFException(); } } while ( true ); } LegacyMasterProcessChannelDecoder( @Nonnull ReadableByteChannel channel ); }
LegacyMasterProcessChannelDecoder implements MasterProcessChannelDecoder { @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) public Command decode() throws IOException { List<String> tokens = new ArrayList<>( 3 ); StringBuilder token = new StringBuilder( MAGIC_NUMBER.length() ); ByteBuffer buffer = ByteBuffer.allocate( 1 ); start: do { boolean endOfStream; tokens.clear(); token.setLength( 0 ); FrameCompletion completion = null; for ( boolean frameStarted = false; !( endOfStream = channel.read( buffer ) == -1 ); completion = null ) { buffer.flip(); char c = (char) buffer.get(); buffer.clear(); if ( !frameStarted ) { if ( c == ':' ) { frameStarted = true; token.setLength( 0 ); tokens.clear(); } } else { if ( c == ':' ) { tokens.add( token.toString() ); token.setLength( 0 ); completion = frameCompleteness( tokens ); if ( completion == FrameCompletion.COMPLETE ) { break; } else if ( completion == FrameCompletion.MALFORMED ) { DumpErrorSingleton.getSingleton() .dumpStreamText( "Malformed frame with tokens " + tokens ); continue start; } } else { token.append( c ); } } } if ( completion == FrameCompletion.COMPLETE ) { MasterProcessCommand cmd = COMMAND_OPCODES.get( tokens.get( 1 ) ); if ( tokens.size() == 2 ) { return new Command( cmd ); } else if ( tokens.size() == 3 ) { return new Command( cmd, tokens.get( 2 ) ); } } if ( endOfStream ) { throw new EOFException(); } } while ( true ); } LegacyMasterProcessChannelDecoder( @Nonnull ReadableByteChannel channel ); @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) Command decode(); @Override void close(); }
LegacyMasterProcessChannelDecoder implements MasterProcessChannelDecoder { @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) public Command decode() throws IOException { List<String> tokens = new ArrayList<>( 3 ); StringBuilder token = new StringBuilder( MAGIC_NUMBER.length() ); ByteBuffer buffer = ByteBuffer.allocate( 1 ); start: do { boolean endOfStream; tokens.clear(); token.setLength( 0 ); FrameCompletion completion = null; for ( boolean frameStarted = false; !( endOfStream = channel.read( buffer ) == -1 ); completion = null ) { buffer.flip(); char c = (char) buffer.get(); buffer.clear(); if ( !frameStarted ) { if ( c == ':' ) { frameStarted = true; token.setLength( 0 ); tokens.clear(); } } else { if ( c == ':' ) { tokens.add( token.toString() ); token.setLength( 0 ); completion = frameCompleteness( tokens ); if ( completion == FrameCompletion.COMPLETE ) { break; } else if ( completion == FrameCompletion.MALFORMED ) { DumpErrorSingleton.getSingleton() .dumpStreamText( "Malformed frame with tokens " + tokens ); continue start; } } else { token.append( c ); } } } if ( completion == FrameCompletion.COMPLETE ) { MasterProcessCommand cmd = COMMAND_OPCODES.get( tokens.get( 1 ) ); if ( tokens.size() == 2 ) { return new Command( cmd ); } else if ( tokens.size() == 3 ) { return new Command( cmd, tokens.get( 2 ) ); } } if ( endOfStream ) { throw new EOFException(); } } while ( true ); } LegacyMasterProcessChannelDecoder( @Nonnull ReadableByteChannel channel ); @Override @Nonnull @SuppressWarnings( "checkstyle:innerassignment" ) Command decode(); @Override void close();  }