@Test public void shouldDecode3BytesEncodedSymbol() throws Exception { byte[] encodedSymbol = new byte[] {(byte) -30, (byte) -126, (byte) -84}; int countSymbols = 1024; byte[] input = new byte[encodedSymbol.length * countSymbols]; for ( int i = 0; i < countSymbols; i++ ) { System.arraycopy( encodedSymbol, 0, input, encodedSymbol.length * i, encodedSymbol.length ); } Channel channel = new Channel( input, 2 ); EventConsumerThread thread = new EventConsumerThread( "t", channel, new MockEventHandler<Event>(), COUNTDOWN_CLOSEABLE, new MockForkNodeArguments() ); Memento memento = thread.new Memento(); memento.bb.position( 0 ).limit( 0 ); String decodedOutput = thread.readString( memento, input.length ); assertThat( decodedOutput ) .isEqualTo( new String( input, 0, input.length, UTF_8 ) ); }
protected String readString( Memento memento ) throws IOException, MalformedFrameException { memento.cb.clear(); int readCount = readInt( memento ); read( memento, readCount + DELIMITER_LENGTH ); final String string; if ( readCount == 0 ) { string = ""; } else if ( readCount == 1 ) { read( memento, 1 ); byte oneChar = memento.bb.get(); string = oneChar == 0 ? null : String.valueOf( (char) oneChar ); } else { string = readString( memento, readCount ); } checkDelimiter( memento ); return string; }
EventConsumerThread extends CloseableDaemonThread { protected String readString( Memento memento ) throws IOException, MalformedFrameException { memento.cb.clear(); int readCount = readInt( memento ); read( memento, readCount + DELIMITER_LENGTH ); final String string; if ( readCount == 0 ) { string = ""; } else if ( readCount == 1 ) { read( memento, 1 ); byte oneChar = memento.bb.get(); string = oneChar == 0 ? null : String.valueOf( (char) oneChar ); } else { string = readString( memento, readCount ); } checkDelimiter( memento ); return string; } }
EventConsumerThread extends CloseableDaemonThread { protected String readString( Memento memento ) throws IOException, MalformedFrameException { memento.cb.clear(); int readCount = readInt( memento ); read( memento, readCount + DELIMITER_LENGTH ); final String string; if ( readCount == 0 ) { string = ""; } else if ( readCount == 1 ) { read( memento, 1 ); byte oneChar = memento.bb.get(); string = oneChar == 0 ? null : String.valueOf( (char) oneChar ); } else { string = readString( memento, readCount ); } checkDelimiter( memento ); return string; } EventConsumerThread( @Nonnull String threadName,
                                @Nonnull ReadableByteChannel channel,
                                @Nonnull EventHandler<Event> eventHandler,
                                @Nonnull CountdownCloseable countdownCloseable,
                                @Nonnull ForkNodeArguments arguments ); }
EventConsumerThread extends CloseableDaemonThread { protected String readString( Memento memento ) throws IOException, MalformedFrameException { memento.cb.clear(); int readCount = readInt( memento ); read( memento, readCount + DELIMITER_LENGTH ); final String string; if ( readCount == 0 ) { string = ""; } else if ( readCount == 1 ) { read( memento, 1 ); byte oneChar = memento.bb.get(); string = oneChar == 0 ? null : String.valueOf( (char) oneChar ); } else { string = readString( memento, readCount ); } checkDelimiter( memento ); return string; } EventConsumerThread( @Nonnull String threadName,
                                @Nonnull ReadableByteChannel channel,
                                @Nonnull EventHandler<Event> eventHandler,
                                @Nonnull CountdownCloseable countdownCloseable,
                                @Nonnull ForkNodeArguments arguments ); @Override void run(); @Override void disable(); @Override void close(); }
EventConsumerThread extends CloseableDaemonThread { protected String readString( Memento memento ) throws IOException, MalformedFrameException { memento.cb.clear(); int readCount = readInt( memento ); read( memento, readCount + DELIMITER_LENGTH ); final String string; if ( readCount == 0 ) { string = ""; } else if ( readCount == 1 ) { read( memento, 1 ); byte oneChar = memento.bb.get(); string = oneChar == 0 ? null : String.valueOf( (char) oneChar ); } else { string = readString( memento, readCount ); } checkDelimiter( memento ); return string; } EventConsumerThread( @Nonnull String threadName,
                                @Nonnull ReadableByteChannel channel,
                                @Nonnull EventHandler<Event> eventHandler,
                                @Nonnull CountdownCloseable countdownCloseable,
                                @Nonnull ForkNodeArguments arguments ); @Override void run(); @Override void disable(); @Override void close();  }