Package io.netty.buffer

Examples of io.netty.buffer.ChannelBuffer


    private HttpChunk encodeNextChunkUrlEncoded(int sizeleft) throws ErrorDataEncoderException {
        if (currentData == null) {
            return null;
        }
        int size = sizeleft;
        ChannelBuffer buffer;
        if (isKey) {
            // get name
            String key = currentData.getName();
            buffer = ChannelBuffers.wrappedBuffer(key.getBytes());
            isKey = false;
            if (currentBuffer == null) {
                currentBuffer = ChannelBuffers.wrappedBuffer(
                        buffer, ChannelBuffers.wrappedBuffer("=".getBytes()));
                //continue
                size -= buffer.readableBytes() + 1;
            } else {
                currentBuffer = ChannelBuffers.wrappedBuffer(currentBuffer,
                    buffer, ChannelBuffers.wrappedBuffer("=".getBytes()));
                //continue
                size -= buffer.readableBytes() + 1;
            }
            if (currentBuffer.readableBytes() >= HttpPostBodyUtil.chunkSize) {
                buffer = fillChannelBuffer();
                return new DefaultHttpChunk(buffer);
            }
        }
        try {
            buffer = ((Attribute) currentData).getChunk(size);
        } catch (IOException e) {
            throw new ErrorDataEncoderException(e);
        }
        ChannelBuffer delimiter = null;
        if (buffer.readableBytes() < size) {
            // delimiter
            isKey = true;
            delimiter = iterator.hasNext() ?
                    ChannelBuffers.wrappedBuffer("&".getBytes()) :
View Full Code Here


    public HttpChunk nextChunk() throws ErrorDataEncoderException {
        if (isLastChunk) {
            isLastChunkSent = true;
            return new DefaultHttpChunk(ChannelBuffers.EMPTY_BUFFER);
        }
        ChannelBuffer buffer = null;
        int size = HttpPostBodyUtil.chunkSize;
        // first test if previous buffer is not empty
        if (currentBuffer != null) {
            size -= currentBuffer.readableBytes();
        }
View Full Code Here

            m.setHeader(
                    HttpHeaders.Names.CONTENT_ENCODING,
                    result.getTargetContentEncoding());

            if (!m.isChunked()) {
                ChannelBuffer content = m.getContent();
                // Encode the content.
                content = ChannelBuffers.wrappedBuffer(
                        encode(content), finishEncode());

                // Replace the content.
                m.setContent(content);
                if (m.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
                    m.setHeader(
                            HttpHeaders.Names.CONTENT_LENGTH,
                            Integer.toString(content.readableBytes()));
                }
            }

            // Because HttpMessage is a mutable object, we can simply forward the write request.
            ctx.sendDownstream(e);
        } else if (msg instanceof HttpChunk) {
            HttpChunk c = (HttpChunk) msg;
            ChannelBuffer content = c.getContent();

            // Encode the chunk if necessary.
            if (encoder != null) {
                if (!c.isLast()) {
                    content = encode(content);
                    if (content.readable()) {
                        c.setContent(content);
                        ctx.sendDownstream(e);
                    }
                } else {
                    ChannelBuffer lastProduct = finishEncode();

                    // Generate an additional chunk if the decoder produced
                    // the last product on closure,
                    if (lastProduct.readable()) {
                        Channels.write(
                                ctx, Channels.succeededFuture(e.getChannel()), new DefaultHttpChunk(lastProduct), e.getRemoteAddress());
                    }

                    // Emit the last chunk.
View Full Code Here

        encoder.offer(buf);
        return ChannelBuffers.wrappedBuffer(encoder.pollAll(new ChannelBuffer[encoder.size()]));
    }

    private ChannelBuffer finishEncode() {
        ChannelBuffer result;
        if (encoder.finish()) {
            result = ChannelBuffers.wrappedBuffer(encoder.pollAll(new ChannelBuffer[encoder.size()]));
        } else {
            result = ChannelBuffers.EMPTY_BUFFER;
        }
View Full Code Here

        int frameSize = delimPos - ridx;
        if (frameSize > maxFrameSize) {
            throw new TooLongFrameException();
        }

        ChannelBuffer binaryData = buffer.readBytes(frameSize);
        buffer.skipBytes(1);

        int ffDelimPos = binaryData.indexOf(binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
        if (ffDelimPos >= 0) {
            throw new IllegalArgumentException("a text frame should not contain 0xFF.");
        }

        return new TextWebSocketFrame(binaryData);
View Full Code Here

            checkpoint(State.PAYLOAD);
        case PAYLOAD:
            // Sometimes, the payload may not be delivered in 1 nice packet
            // We need to accumulate the data until we have it all
            int rbytes = actualReadableBytes();
            ChannelBuffer payloadBuffer = null;

            int willHaveReadByteCount = framePayloadBytesRead + rbytes;
            // logger.debug("Frame rbytes=" + rbytes + " willHaveReadByteCount="
            // + willHaveReadByteCount + " framePayloadLength=" +
            // framePayloadLength);
View Full Code Here

        byte[] mask;

        if (msg instanceof WebSocketFrame) {
            WebSocketFrame frame = (WebSocketFrame) msg;
            ChannelBuffer data = frame.getBinaryData();
            if (data == null) {
                data = ChannelBuffers.EMPTY_BUFFER;
            }

            byte opcode;
            if (frame instanceof TextWebSocketFrame) {
                opcode = OPCODE_TEXT;
            } else if (frame instanceof PingWebSocketFrame) {
                opcode = OPCODE_PING;
            } else if (frame instanceof PongWebSocketFrame) {
                opcode = OPCODE_PONG;
            } else if (frame instanceof CloseWebSocketFrame) {
                opcode = OPCODE_CLOSE;
            } else if (frame instanceof BinaryWebSocketFrame) {
                opcode = OPCODE_BINARY;
            } else if (frame instanceof ContinuationWebSocketFrame) {
                opcode = OPCODE_CONT;
            } else {
                throw new UnsupportedOperationException("Cannot encode frame of type: " + frame.getClass().getName());
            }

            int length = data.readableBytes();

            if (logger.isDebugEnabled()) {
                logger.debug("Encoding WebSocket Frame opCode=" + opcode + " length=" + length);
            }

            int b0 = 0;
            if (frame.isFinalFragment()) {
                b0 |= 1 << 7;
            }
            b0 |= frame.getRsv() % 8 << 4;
            b0 |= opcode % 128;

            ChannelBuffer header;
            ChannelBuffer body;

            if (opcode == OPCODE_PING && length > 125) {
                throw new TooLongFrameException("invalid payload for PING (payload length must be <= 125, was "
                        + length);
            }

            int maskLength = maskPayload ? 4 : 0;
            if (length <= 125) {
                header = ChannelBuffers.buffer(2 + maskLength);
                header.writeByte(b0);
                byte b = (byte) (maskPayload ? 0x80 | (byte) length : (byte) length);
                header.writeByte(b);
            } else if (length <= 0xFFFF) {
                header = ChannelBuffers.buffer(4 + maskLength);
                header.writeByte(b0);
                header.writeByte(maskPayload ? 0xFE : 126);
                header.writeByte(length >>> 8 & 0xFF);
                header.writeByte(length & 0xFF);
            } else {
                header = ChannelBuffers.buffer(10 + maskLength);
                header.writeByte(b0);
                header.writeByte(maskPayload ? 0xFF : 127);
                header.writeLong(length);
            }

            // Write payload
            if (maskPayload) {
                Integer random = (int) (Math.random() * Integer.MAX_VALUE);
                mask = ByteBuffer.allocate(4).putInt(random).array();
                header.writeBytes(mask);

                body = ChannelBuffers.buffer(length);
                int counter = 0;
                while (data.readableBytes() > 0) {
                    byte byteData = data.readByte();
                    body.writeByte(byteData ^ mask[+counter++ % 4]);
                }
            } else {
                body = data;
            }
            return ChannelBuffers.wrappedBuffer(header, body);
View Full Code Here

            String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
            String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
            int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
            int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
            long c = req.getContent().readLong();
            ChannelBuffer input = ChannelBuffers.buffer(16);
            input.writeInt(a);
            input.writeInt(b);
            input.writeLong(c);
            ChannelBuffer output = ChannelBuffers.wrappedBuffer(WebSocketUtil.md5(input.array()));
            res.setContent(output);
        } else {
            // Old Hixie 75 handshake method with no challenge:
            res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
            res.addHeader(WEBSOCKET_LOCATION, getWebSocketUrl());
View Full Code Here

TOP

Related Classes of io.netty.buffer.ChannelBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.