public void finish() throws CorruptedFrameException { checking = false; codep = 0; if (state != UTF8_ACCEPT) { state = UTF8_ACCEPT; throw new CorruptedFrameException("bytes are not UTF-8"); } }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { try { if (buffer.readableBytes() == 0) return null; // Validate the opcode (this will throw if it's not a response) if (buffer.readableBytes() >= 4) Message.Response.Type.fromOpcode(buffer.getByte(3)); ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer); if (frame == null) { return null; } return Frame.create(frame); } catch (CorruptedFrameException e) { throw new DriverInternalError(e.getMessage()); } catch (TooLongFrameException e) { throw new DriverInternalError(e.getMessage()); } } }
private void write(int b) throws CorruptedFrameException { byte type = TYPES[b & 0xFF]; codep = state != UTF8_ACCEPT ? b & 0x3f | codep << 6 : 0xff >> type & b; state = STATES[state + type]; if (state == UTF8_REJECT) { throw new CorruptedFrameException("bytes are not UTF-8"); } }
private void protocolViolation(Channel channel, String reason) throws CorruptedFrameException { protocolViolation(channel, new CorruptedFrameException(reason)); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { buffer.markReaderIndex(); final byte[] buf = new byte[5]; for (int i = 0; i < buf.length; i ++) { if (!buffer.readable()) { buffer.resetReaderIndex(); return null; } buf[i] = buffer.readByte(); if (buf[i] >= 0) { int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32(); if (length < 0) { throw new CorruptedFrameException("negative length: " + length); } if (buffer.readableBytes() < length) { buffer.resetReaderIndex(); return null; } else { return buffer.readBytes(length); } } } // Couldn't find the byte whose MSB is off. throw new CorruptedFrameException("length wider than 32-bit"); } }
throw new CorruptedFrameException( "negative pre-adjustment length field: " + frameLength); if (frameLength < lengthFieldEndOffset) { buffer.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException( "Adjusted frame length (" + frameLength + ") is less " + "than lengthFieldEndOffset: " + lengthFieldEndOffset); throw new CorruptedFrameException( "Adjusted frame length (" + frameLength + ") is less " + "than initialBytesToStrip: " + initialBytesToStrip);
private void protocolViolation(Channel channel, String reason) throws CorruptedFrameException { checkpoint(CORRUPT); if (channel.isConnected()) { channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); channel.close().awaitUninterruptibly(); } throw new CorruptedFrameException(reason); }
private void protocolViolation(Channel channel, String reason) throws CorruptedFrameException { checkpoint(CORRUPT); if (channel.isConnected()) { channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); channel.close().awaitUninterruptibly(); } throw new CorruptedFrameException(reason); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (buffer.readableBytes() < HEAD_LENGHT) { return null; } buffer.markReaderIndex(); byte tag = buffer.readByte(); if (tag != PACKAGE_TAG) { throw new CorruptedFrameException("非法协议包"); } byte encode = buffer.readByte(); byte encrypt = buffer.readByte(); byte extend1 = buffer.readByte(); byte extend2 = buffer.readByte(); byte sessionByte[] = new byte[32]; buffer.readBytes(sessionByte); String sessionid = new String(sessionByte); int length = buffer.readInt(); int commandId = buffer.readInt(); if (buffer.readableBytes() < length) { buffer.resetReaderIndex(); return null; } Header header = new Header(encode, encrypt, extend1, extend2, sessionid, length, commandId); Message message = new Message(header, buffer.readBytes(length)); return message; }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (buffer.readableBytes() < HEAD_LENGHT) { return null; } buffer.markReaderIndex(); byte tag = buffer.readByte(); if (tag != PACKAGE_TAG) { throw new CorruptedFrameException("非法协议包"); } byte encode = buffer.readByte(); byte encrypt = buffer.readByte(); byte extend1 = buffer.readByte(); byte extend2 = buffer.readByte(); int sessionid = buffer.readInt(); int length = buffer.readInt(); int commandId = buffer.readInt(); if (buffer.readableBytes() < length) { buffer.resetReaderIndex(); return null; } Header header = new Header(encode, encrypt, extend1, extend2, sessionid, length, commandId); Message message = new Message(header, buffer.readBytes(length).array()); return message; }