@Test public void shouldReadString() throws Exception { Channel channel = new Channel( PATTERN1.getBytes(), PATTERN1.length() ); 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 s = thread.readString( memento, 10 ); assertThat( s ) .isEqualTo( "0123456789" ); }
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();  }