Search in sources :

Example 1 with ByteArrayBuffer

use of com.generallycloud.baseio.component.ByteArrayBuffer in project baseio by generallycloud.

the class ServerHTTPProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture readFuture) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ServerHttpFuture f = (ServerHttpFuture) readFuture;
    if (f.isUpdateWebSocketProtocol()) {
        channel.setProtocolDecoder(WebSocketProtocolFactory.WS_PROTOCOL_DECODER);
        channel.setProtocolEncoder(WebSocketProtocolFactory.WS_PROTOCOL_ENCODER);
        channel.setProtocolFactory(WebSocketProtocolFactory.WS_PROTOCOL_FACTORY);
        channel.getSession().setAttribute(WebSocketFuture.SESSION_KEY_SERVICE_NAME, f.getFutureName());
    }
    f.setResponseHeader("Date", HttpHeaderDateFormat.getFormat().format(System.currentTimeMillis()));
    ByteArrayBuffer os = f.getBinaryBuffer();
    if (os != null) {
        encode(allocator, f, os.size(), os.array());
        return;
    }
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        encode(allocator, f, 0, null);
        return;
    }
    encode(allocator, f, writeSize, f.getWriteBuffer());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ServerHttpFuture(com.generallycloud.baseio.codec.http11.future.ServerHttpFuture) ByteArrayBuffer(com.generallycloud.baseio.component.ByteArrayBuffer)

Example 2 with ByteArrayBuffer

use of com.generallycloud.baseio.component.ByteArrayBuffer in project baseio by generallycloud.

the class CharBasedFutureImpl method read.

@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
    if (complete) {
        return true;
    }
    ByteArrayBuffer cache = this.cache;
    for (; buffer.hasRemaining(); ) {
        byte b = buffer.getByte();
        if (b == splitor) {
            this.readText = cache.toString(context.getEncoding());
            this.complete = true;
            return true;
        }
        cache.write(b);
        if (cache.size() > limit) {
            throw new IOException("max length " + limit);
        }
    }
    return false;
}
Also used : IOException(java.io.IOException) ByteArrayBuffer(com.generallycloud.baseio.component.ByteArrayBuffer)

Aggregations

ByteArrayBuffer (com.generallycloud.baseio.component.ByteArrayBuffer)2 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 ServerHttpFuture (com.generallycloud.baseio.codec.http11.future.ServerHttpFuture)1 IOException (java.io.IOException)1