ByteBuf buf = ByteBuf.createExact(8); //Add the short to the array buf.add((short)77); byte[] bytes = buf.readAndReset(); boolean worked = true; //Read the short back int value = idxShort(bytes, 0); worked |= buf.len() == 2 || die("short worked length = 2"); worked |= value == 77 || die("short worked value was 77");
@Override public void writeLargeDoubleArray( double[] values ) { int byteSize = values.length * 8 + 4; this.add( values.length ); doWriteDoubleArray( values, byteSize ); }
@Override public void writeMediumByteArray( byte[] bytes ) { this.addUnsignedShort( bytes.length ); this.add( bytes ); }
@Override public void writeLargeLongArray( long[] values ) { int byteSize = values.length * 8 + 4; this.add( values.length ); doWriteLongArray( values, byteSize ); }
buf.addByte ( '&' ); buf.addUrlEncoded ( key ); buf.addByte ( '=' ); buf.addUrlEncoded ( value.toString () ); } else { buf.addUrlEncodedByteArray((byte[]) value); int len = buf.len (); IO.write(connection.getOutputStream(), new String(buf.readForRecycle (), 0, len, StandardCharsets.UTF_8), IO.DEFAULT_CHARSET); return connection;
@Test public void test4() { ByteBuf buf = new ByteBuf(); buf.writeUnsignedShort( Short.MAX_VALUE + 1 ); buf.writeUnsignedInt( ( ( long ) Integer.MAX_VALUE ) + 1L ); buf.writeUnsignedByte( ( short ) ( Byte.MAX_VALUE + 1 ) ); buf.writeInt( 1 ); buf.writeBoolean( true ); buf.writeBoolean( false ); buf.writeFloat( 1.0f ); buf.writeDouble( 1.0d ); buf.writeChar( 'a' ); buf.writeLong( 1L ); buf.writeSmallString( "hi mom" ); buf.writeMediumString( "hi mom" ); buf.writeLargeString( "AAA" ); buf.writeLargeString( "BBB" ); buf.writeByte( ( byte ) 7 ); buf.writeShort( ( short ) 11 ); buf.writeSmallByteArray( new byte[]{ 1, 2, 3 } ); buf.writeMediumByteArray( new byte[]{ 1, 2, 3 } ); buf.writeLargeByteArray( new byte[]{ 1, 2, 3 } ); buf.writeSmallShortArray( new short[]{ 1, 2, 3 } ); buf.writeSmallShortArray( new short[]{ 1, 2, 3 } ); buf.writeLargeShortArray( new short[]{ 1, 2, 3 } ); buf.writeMediumShortArray( new short[]{ 2, 2, 2 } ); buf.writeSmallIntArray( new int[]{ 1, 2, 3 } ); buf.writeLargeIntArray( new int[]{ 1, 2, 3 } ); buf.writeMediumIntArray( new int[]{ 2, 2, 2 } ); buf.writeSmallLongArray( new long[]{ 1, 2, 3 } );
@Test public void testExact() { ByteBuf buf = ByteBuf.createExact( 66 ); buf.add( bytes( "0123456789\n" ) ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456END\n" ); String out = new String( buf.readAndReset() ); assertEquals( 66, buf.len() ); assertTrue( out.endsWith( "END\n" ) ); }
ByteBuf buf = ByteBuf.create( 20 ); buf.addByte( 0xAA ); buf.addByte( 0x78 ); buf.addByte( 0x2D ); buf.addByte( 0x53 ); byte[] bytes = buf.readForRecycle(); buf.add( 2860002643L ); bytes = buf.readForRecycle(); buf.addUnsignedInt( 2860002643L ); bytes = buf.readForRecycle();
@Test public void test3() throws UnsupportedEncodingException { ByteBuf buf = new ByteBuf(); buf.add( URLEncoder.encode( "abc", "UTF-8" ) ); buf.addUrlEncodedByteArray( new byte[]{ ( byte ) 1, 2, 3 } ); buf.add( URLEncoder.encode( "def", "UTF-8" ) ); boolean ok = true; final byte[] bytes = buf.readForRecycle(); ok |= bytes[ 3 ] == '%' || die(); ok |= bytes[ 4 ] == '0' || die(); ok |= bytes[ 5 ] == '1' || die(); ok |= bytes[ 6 ] == '%' || die( "" + bytes[ 5 ] ); ok |= bytes[ 7 ] == '0' || die(); ok |= bytes[ 8 ] == '2' || die(); ok |= bytes[ 9 ] == '%' || die(); ok |= bytes[ 10 ] == '0' || die(); ok |= bytes[ 11 ] == '3' || die(); ok |= bytes[ 12 ] == 'd' || die(); }
@Test public void testMe() { ByteBuf buf = new ByteBuf(); buf.add( bytes( "0123456789\n" ) ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456789\n" ); buf.add( "0123456END\n" ); String out = new String( buf.readAndReset(), 0, buf.len() ); assertEquals( 66, buf.len() ); assertTrue( out.endsWith( "END\n" ) ); }
public static byte[] input( InputStream inputStream ) { try ( InputStream is = inputStream ) { ByteBuf buf = ByteBuf.create( DEFAULT_BUFFER_SIZE ); byte[] bytes = new byte[ DEFAULT_BUFFER_SIZE ]; int read = -2; while ( read != -1 ) { read = inputStream.read( bytes ); if ( read == DEFAULT_BUFFER_SIZE ) { buf.add( bytes ); } else if ( read > 0 ) { buf.add( bytes, read ); } } return buf.toBytes(); } catch ( Exception ex ) { return Exceptions.handle( byte[].class, ex ); } }
@Test public void test2() { ByteBuf buf = new ByteBuf(); buf.add( "abc" ); buf.add( "def" ); boolean ok = true; final byte[] bytes = buf.readForRecycle(); ok |= bytes[ 3 ] == 'd' || die(); }
this.add( "null" ); return this; this.addByte( '"' ); this.addByte( '\\' ); this.addByte( '"' ); break; this.addByte( '\\' ); this.addByte( '\\' ); break; this.addByte( '\\' ); this.addByte( '/' ); break; this.addByte( '\\' ); this.addByte( 'n' ); break; this.addByte( '\\' ); this.addByte( 't' ); break; this.addByte( '\\' ); this.addByte( 'r' ); break;
@Test public void addRockSockEmRobotEm() { boolean worked = true; ByteBuf buf = ByteBuf.create( 1 ); buf.add( ( byte ) 1 ); buf.add( ( short ) 2 ); buf.add( ( char ) 3 ); buf.add( 4 ); buf.add( ( float ) 5 ); buf.add( ( long ) 6 ); buf.add( ( double ) 7 ); worked |= buf.len() == 29 || die( "length = 29" ); byte[] bytes = buf.readAndReset(); worked |= myDouble == 7 || die( "value was 7" ); ByteBuf buf2 = ByteBuf.create( bytes ); bytes = buf2.readAndReset();
@Override public void writeSmallByteArray( byte[] bytes ) { this.addUnsignedByte( ( short ) bytes.length ); this.add( bytes ); }
@Test public void testURLEncodeBytes() { ByteBuf buf = ByteBuf.create( 20 ); buf.addUrlEncodedByteArray( new byte[]{ 0x0, 0x1, 0x2 } ); final byte[] bytes = buf.readForRecycle(); boolean ok = true; ok |= bytes[ 0 ] == '%' || die(); ok |= bytes[ 1 ] == '0' || die(); ok |= bytes[ 2 ] == '0' || die(); ok |= bytes[ 3 ] == '%' || die(); ok |= bytes[ 4 ] == '0' || die(); ok |= bytes[ 5 ] == '1' || die(); ok |= bytes[ 6 ] == '%' || die(); ok |= bytes[ 7 ] == '0' || die(); ok |= bytes[ 8 ] == '2' || die(); }