Search in sources :

Example 56 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class Http2Codec method encode.

@Override
public ByteBuf encode(Channel ch, Frame frame) {
    Http2Frame f = (Http2Frame) frame;
    Http2FrameType frameType = f.getHttp2FrameType();
    byte[] payload = null;
    switch(frameType) {
        case FRAME_TYPE_CONTINUATION:
            break;
        case FRAME_TYPE_DATA:
            break;
        case FRAME_TYPE_GOAWAY:
            break;
        case FRAME_TYPE_HEADERS:
            Http2HeadersFrame hf = (Http2HeadersFrame) f;
            break;
        case FRAME_TYPE_PING:
            break;
        case FRAME_TYPE_PRIORITY:
            break;
        case FRAME_TYPE_PUSH_PROMISE:
            break;
        case FRAME_TYPE_RST_STREAM:
            break;
        case FRAME_TYPE_SETTINGS:
            Http2SettingsFrame sf = (Http2SettingsFrame) f;
            long[] settings = sf.getSettings();
            payload = new byte[6 * 6];
            for (int i = 0; i < 6; i++) {
                int realI = i + 1;
                int offset = i * 6;
                ByteUtil.putShort(payload, (short) realI, offset);
                ByteUtil.putInt(payload, (int) settings[realI], offset + 2);
            }
            break;
        case FRAME_TYPE_WINDOW_UPDATE:
            break;
        default:
            break;
    }
    int length = payload.length;
    ByteBuf buf = ch.alloc().allocate(length + PROTOCOL_HEADER);
    byte b2 = (byte) ((length & 0xff));
    byte b1 = (byte) ((length >> 8 * 1) & 0xff);
    byte b0 = (byte) ((length >> 8 * 2) & 0xff);
    byte b3 = frameType.getByteValue();
    buf.writeByte(b0);
    buf.writeByte(b1);
    buf.writeByte(b2);
    buf.writeByte(b3);
    buf.writeByte((byte) 0);
    buf.writeInt(f.getStreamIdentifier());
    buf.writeBytes(payload);
    return buf;
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 57 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestMask method main.

public static void main(String[] args) {
    ByteBuf buf = ByteBuf.wrap("hello word!".getBytes());
    byte mask = (byte) buf.readableBytes();
    mask(buf, mask);
    System.out.println(new String(buf.readBytes()));
    buf.readIndex(0);
    mask(buf, mask);
    System.out.println(new String(buf.readBytes()));
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 58 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class Lz4CompressedInputStream method read.

@Override
public int read(byte[] b, int off, int len) throws IOException {
    ByteBuf buf = this.buf;
    byte[] readBuffer = buf.array();
    int limit = buf.writeIndex();
    int offset = buf.readIndex();
    if (buf.readableBytes() <= 4) {
        if (!hasRemaining) {
            return -1;
        }
        read(buf);
        return read(b, off, len);
    }
    int _len = ByteUtil.getInt(readBuffer, offset);
    offset += 4;
    if (limit - offset < _len) {
        if (!hasRemaining) {
            return -1;
        }
        read(buf);
        return read(b, off, len);
    }
    buf.readIndex(offset + _len);
    return Lz4RawDecompressor.decompress(readBuffer, offset, _len, b, off, len);
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)58 Test (org.junit.Test)18 ByteBufAllocator (com.firenio.buffer.ByteBufAllocator)5 Channel (com.firenio.component.Channel)5 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)4 Frame (com.firenio.component.Frame)3 IoEventHandle (com.firenio.component.IoEventHandle)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 ChannelConnector (com.firenio.component.ChannelConnector)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 ByteBufAllocatorGroup (com.firenio.buffer.ByteBufAllocatorGroup)1 PoolState (com.firenio.buffer.PooledByteBufAllocator.PoolState)1