Created
December 21, 2017 10:37
-
-
Save johnou/b15c98dc34090f445068a787f40d69e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2012 The Netty Project | |
* | |
* The Netty Project licenses this file to you under the Apache License, | |
* version 2.0 (the "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package io.netty.handler.codec; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.channel.ChannelHandler.Sharable; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.channel.ChannelOutboundHandler; | |
import io.netty.channel.ChannelOutboundHandlerAdapter; | |
import io.netty.channel.ChannelPipeline; | |
import io.netty.channel.ChannelPromise; | |
import io.netty.util.internal.ObjectUtil; | |
import java.nio.ByteOrder; | |
/** | |
* An encoder that prepends the length of the message. The length value is | |
* prepended as a binary form. | |
* <p> | |
* For example, <tt>{@link LengthFieldPrepender}(2)</tt> will encode the | |
* following 12-bytes string: | |
* <pre> | |
* +----------------+ | |
* | "HELLO, WORLD" | | |
* +----------------+ | |
* </pre> | |
* into the following: | |
* <pre> | |
* +--------+----------------+ | |
* + 0x000C | "HELLO, WORLD" | | |
* +--------+----------------+ | |
* </pre> | |
* If you turned on the {@code lengthIncludesLengthFieldLength} flag in the | |
* constructor, the encoded data would look like the following | |
* (12 (original data) + 2 (prepended data) = 14 (0xE)): | |
* <pre> | |
* +--------+----------------+ | |
* + 0x000E | "HELLO, WORLD" | | |
* +--------+----------------+ | |
* </pre> | |
*/ | |
@Sharable | |
public class LengthFieldPrepender extends ChannelOutboundHandlerAdapter { | |
private final ByteOrder byteOrder; | |
private final int lengthFieldLength; | |
private final boolean lengthIncludesLengthFieldLength; | |
private final int lengthAdjustment; | |
/** | |
* Creates a new instance. | |
* | |
* @param lengthFieldLength the length of the prepended length field. | |
* Only 1, 2, 3, 4, and 8 are allowed. | |
* | |
* @throws IllegalArgumentException | |
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 | |
*/ | |
public LengthFieldPrepender(int lengthFieldLength) { | |
this(lengthFieldLength, false); | |
} | |
/** | |
* Creates a new instance. | |
* | |
* @param lengthFieldLength the length of the prepended length field. | |
* Only 1, 2, 3, 4, and 8 are allowed. | |
* @param lengthIncludesLengthFieldLength | |
* if {@code true}, the length of the prepended | |
* length field is added to the value of the | |
* prepended length field. | |
* | |
* @throws IllegalArgumentException | |
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 | |
*/ | |
public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength) { | |
this(lengthFieldLength, 0, lengthIncludesLengthFieldLength); | |
} | |
/** | |
* Creates a new instance. | |
* | |
* @param lengthFieldLength the length of the prepended length field. | |
* Only 1, 2, 3, 4, and 8 are allowed. | |
* @param lengthAdjustment the compensation value to add to the value | |
* of the length field | |
* | |
* @throws IllegalArgumentException | |
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 | |
*/ | |
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment) { | |
this(lengthFieldLength, lengthAdjustment, false); | |
} | |
/** | |
* Creates a new instance. | |
* | |
* @param lengthFieldLength the length of the prepended length field. | |
* Only 1, 2, 3, 4, and 8 are allowed. | |
* @param lengthAdjustment the compensation value to add to the value | |
* of the length field | |
* @param lengthIncludesLengthFieldLength | |
* if {@code true}, the length of the prepended | |
* length field is added to the value of the | |
* prepended length field. | |
* | |
* @throws IllegalArgumentException | |
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 | |
*/ | |
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength) { | |
this(ByteOrder.BIG_ENDIAN, lengthFieldLength, lengthAdjustment, lengthIncludesLengthFieldLength); | |
} | |
/** | |
* Creates a new instance. | |
* | |
* @param byteOrder the {@link ByteOrder} of the length field | |
* @param lengthFieldLength the length of the prepended length field. | |
* Only 1, 2, 3, 4, and 8 are allowed. | |
* @param lengthAdjustment the compensation value to add to the value | |
* of the length field | |
* @param lengthIncludesLengthFieldLength | |
* if {@code true}, the length of the prepended | |
* length field is added to the value of the | |
* prepended length field. | |
* | |
* @throws IllegalArgumentException | |
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 | |
*/ | |
public LengthFieldPrepender( | |
ByteOrder byteOrder, int lengthFieldLength, | |
int lengthAdjustment, boolean lengthIncludesLengthFieldLength) { | |
if (lengthFieldLength != 1 && lengthFieldLength != 2 && | |
lengthFieldLength != 3 && lengthFieldLength != 4 && | |
lengthFieldLength != 8) { | |
throw new IllegalArgumentException( | |
"lengthFieldLength must be either 1, 2, 3, 4, or 8: " + | |
lengthFieldLength); | |
} | |
ObjectUtil.checkNotNull(byteOrder, "byteOrder"); | |
this.byteOrder = byteOrder; | |
this.lengthFieldLength = lengthFieldLength; | |
this.lengthIncludesLengthFieldLength = lengthIncludesLengthFieldLength; | |
this.lengthAdjustment = lengthAdjustment; | |
} | |
/** | |
* Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next | |
* {@link ChannelOutboundHandler} in the {@link ChannelPipeline}. | |
*/ | |
private static boolean acceptOutboundMessage(Object msg) { | |
return msg instanceof ByteBuf; | |
} | |
@Override | |
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | |
try { | |
if (acceptOutboundMessage(msg)) { | |
@SuppressWarnings("unchecked") | |
ByteBuf cast = (ByteBuf) msg; | |
int length = cast.readableBytes() + lengthAdjustment; | |
if (lengthIncludesLengthFieldLength) { | |
length += lengthFieldLength; | |
} | |
if (length < 0) { | |
throw new IllegalArgumentException( | |
"Adjusted frame length (" + length + ") is less than zero"); | |
} | |
switch (lengthFieldLength) { | |
case 1: | |
if (length >= 256) { | |
throw new IllegalArgumentException( | |
"length does not fit into a byte: " + length); | |
} | |
ctx.write(ctx.alloc().buffer(1).order(byteOrder).writeByte((byte) length), ctx.voidPromise()); | |
break; | |
case 2: | |
if (length >= 65536) { | |
throw new IllegalArgumentException( | |
"length does not fit into a short integer: " + length); | |
} | |
ctx.write(ctx.alloc().buffer(2).order(byteOrder).writeShort((short) length), ctx.voidPromise()); | |
break; | |
case 3: | |
if (length >= 16777216) { | |
throw new IllegalArgumentException( | |
"length does not fit into a medium integer: " + length); | |
} | |
ctx.write(ctx.alloc().buffer(3).order(byteOrder).writeMedium(length), ctx.voidPromise()); | |
break; | |
case 4: | |
ctx.write(ctx.alloc().buffer(4).order(byteOrder).writeInt(length), ctx.voidPromise()); | |
break; | |
case 8: | |
ctx.write(ctx.alloc().buffer(8).order(byteOrder).writeLong(length), ctx.voidPromise()); | |
break; | |
default: | |
throw new Error("should not reach here"); | |
} | |
} | |
ctx.write(msg, promise); | |
} catch (EncoderException e) { | |
throw e; | |
} catch (Exception e) { | |
throw new EncoderException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment