@Test public void shouldReadNonDefaultCharset() throws Exception { byte[] stream = ( (char) 10 + ":ISO_8859_1:" ).getBytes( US_ASCII ); Channel channel = new Channel( stream, 1 ); EventConsumerThread thread = new EventConsumerThread( "t", channel, new MockEventHandler<Event>(), COUNTDOWN_CLOSEABLE, new MockForkNodeArguments() ); Memento memento = thread.new Memento(); memento.bb.position( 0 ).limit( 0 ); memento.setCharset( UTF_8 ); assertThat( thread.readCharset( memento ) ) .isNotNull() .isEqualTo( ISO_8859_1 ); }
@Nonnull @SuppressWarnings( "checkstyle:magicnumber" ) protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException { int length = readByte( memento ) & 0xff; read( memento, length + DELIMITER_LENGTH ); ByteBuffer bb = memento.bb; byte[] array = bb.array(); int offset = bb.arrayOffset() + bb.position(); bb.position( 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.line.positionByteBuffer, bb.position() ); } }
EventConsumerThread extends CloseableDaemonThread { @Nonnull @SuppressWarnings( "checkstyle:magicnumber" ) protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException { int length = readByte( memento ) & 0xff; read( memento, length + DELIMITER_LENGTH ); ByteBuffer bb = memento.bb; byte[] array = bb.array(); int offset = bb.arrayOffset() + bb.position(); bb.position( 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.line.positionByteBuffer, bb.position() ); } } }
EventConsumerThread extends CloseableDaemonThread { @Nonnull @SuppressWarnings( "checkstyle:magicnumber" ) protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException { int length = readByte( memento ) & 0xff; read( memento, length + DELIMITER_LENGTH ); ByteBuffer bb = memento.bb; byte[] array = bb.array(); int offset = bb.arrayOffset() + bb.position(); bb.position( 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.line.positionByteBuffer, bb.position() ); } } EventConsumerThread( @Nonnull String threadName,
                                @Nonnull ReadableByteChannel channel,
                                @Nonnull EventHandler<Event> eventHandler,
                                @Nonnull CountdownCloseable countdownCloseable,
                                @Nonnull ForkNodeArguments arguments ); }
EventConsumerThread extends CloseableDaemonThread { @Nonnull @SuppressWarnings( "checkstyle:magicnumber" ) protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException { int length = readByte( memento ) & 0xff; read( memento, length + DELIMITER_LENGTH ); ByteBuffer bb = memento.bb; byte[] array = bb.array(); int offset = bb.arrayOffset() + bb.position(); bb.position( 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.line.positionByteBuffer, bb.position() ); } } 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 { @Nonnull @SuppressWarnings( "checkstyle:magicnumber" ) protected Charset readCharset( Memento memento ) throws IOException, MalformedFrameException { int length = readByte( memento ) & 0xff; read( memento, length + DELIMITER_LENGTH ); ByteBuffer bb = memento.bb; byte[] array = bb.array(); int offset = bb.arrayOffset() + bb.position(); bb.position( 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.line.positionByteBuffer, bb.position() ); } } 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();  }