use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.
the class HttpPostMultipartRequestDecoder method readFileUploadByteMultipartStandard.
/**
* Read a FileUpload data as Byte (Binary) and add the bytes directly to the
* FileUpload. If the delimiter is found, the FileUpload is completed.
*
* @throws NotEnoughDataDecoderException
* Need more chunks but do not reset the readerInder since some
* values will be already added to the FileOutput
* @throws ErrorDataDecoderException
* write IO error occurs with the FileUpload
*/
private void readFileUploadByteMultipartStandard(String delimiter) {
int readerIndex = undecodedChunk.readerIndex();
// found the decoder limit
boolean newLine = true;
int index = 0;
int lastPosition = undecodedChunk.readerIndex();
boolean found = false;
while (undecodedChunk.isReadable()) {
byte nextByte = undecodedChunk.readByte();
if (newLine) {
// Check the delimiter
if (nextByte == delimiter.codePointAt(index)) {
index++;
if (delimiter.length() == index) {
found = true;
break;
}
} else {
newLine = false;
index = 0;
// continue until end of line
if (nextByte == HttpConstants.CR) {
if (undecodedChunk.isReadable()) {
nextByte = undecodedChunk.readByte();
if (nextByte == HttpConstants.LF) {
newLine = true;
index = 0;
lastPosition = undecodedChunk.readerIndex() - 2;
} else {
// save last valid position
lastPosition = undecodedChunk.readerIndex() - 1;
// Unread next byte.
undecodedChunk.readerIndex(lastPosition);
}
}
} else if (nextByte == HttpConstants.LF) {
newLine = true;
index = 0;
lastPosition = undecodedChunk.readerIndex() - 1;
} else {
// save last valid position
lastPosition = undecodedChunk.readerIndex();
}
}
} else {
// continue until end of line
if (nextByte == HttpConstants.CR) {
if (undecodedChunk.isReadable()) {
nextByte = undecodedChunk.readByte();
if (nextByte == HttpConstants.LF) {
newLine = true;
index = 0;
lastPosition = undecodedChunk.readerIndex() - 2;
} else {
// save last valid position
lastPosition = undecodedChunk.readerIndex() - 1;
// Unread next byte.
undecodedChunk.readerIndex(lastPosition);
}
}
} else if (nextByte == HttpConstants.LF) {
newLine = true;
index = 0;
lastPosition = undecodedChunk.readerIndex() - 1;
} else {
// save last valid position
lastPosition = undecodedChunk.readerIndex();
}
}
}
ByteBuf buffer = undecodedChunk.copy(readerIndex, lastPosition - readerIndex);
if (found) {
// found so lastPosition is correct and final
try {
currentFileUpload.addContent(buffer, true);
// just before the CRLF and delimiter
undecodedChunk.readerIndex(lastPosition);
} catch (IOException e) {
throw new ErrorDataDecoderException(e);
}
} else {
// position is OK
try {
currentFileUpload.addContent(buffer, false);
// last valid char (not CR, not LF, not beginning of delimiter)
undecodedChunk.readerIndex(lastPosition);
throw new NotEnoughDataDecoderException();
} catch (IOException e) {
throw new ErrorDataDecoderException(e);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.
the class CloseWebSocketFrame method statusCode.
/**
* Returns the closing status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. If
* a getStatus code is set, -1 is returned.
*/
public int statusCode() {
ByteBuf binaryData = content();
if (binaryData == null || binaryData.capacity() == 0) {
return -1;
}
binaryData.readerIndex(0);
int statusCode = binaryData.readShort();
binaryData.readerIndex(0);
return statusCode;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.
the class SnappyTest method encodeLongTextUsesCopy.
@Test
public void encodeLongTextUsesCopy() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(("Netty has been designed carefully with the experiences " + "earned from the implementation of a lot of protocols " + "such as FTP, SMTP, HTTP, and various binary and " + "text-based legacy protocols").getBytes("US-ASCII"));
ByteBuf out = Unpooled.buffer(180);
snappy.encode(in, out, in.readableBytes());
// The only compressibility in the above are the words "the ",
// and "protocols", so this is a literal, followed by a copy
// followed by another literal, followed by another copy
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { // preamble length
-0x49, // preamble length
0x01, // literal tag + length
-0x10, // literal tag + length
0x42, // Literal
0x4e, 0x65, 0x74, 0x74, 0x79, 0x20, 0x68, 0x61, 0x73, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x64, 0x65, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x63, 0x61, 0x72, 0x65, 0x66, 0x75, 0x6c, 0x6c, 0x79, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x78, 0x70, 0x65, 0x72, 0x69, 0x65, 0x6e, 0x63, 0x65, 0x73, 0x20, 0x65, 0x61, 0x72, 0x6e, 0x65, 0x64, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, // First copy (the)
0x01, 0x1C, -0x10, // Next literal
0x66, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x6c, 0x6f, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x20, 0x73, 0x75, 0x63, 0x68, 0x20, 0x61, 0x73, 0x20, 0x46, 0x54, 0x50, 0x2c, 0x20, 0x53, 0x4d, 0x54, 0x50, 0x2c, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x76, 0x61, 0x72, 0x69, 0x6f, 0x75, 0x73, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x62, 0x61, 0x73, 0x65, 0x64, 0x20, 0x6c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x20, // Second copy (protocols)
0x15, 0x4c });
assertEquals("Encoded result was incorrect", expected, out);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.
the class DatagramUnicastTest method testSimpleSend0.
@SuppressWarnings("deprecation")
private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient, final byte[] bytes, int count) throws Throwable {
final CountDownLatch latch = new CountDownLatch(count);
sb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
ByteBuf buf = msg.content();
assertEquals(bytes.length, buf.readableBytes());
for (byte b : bytes) {
assertEquals(b, buf.readByte());
}
latch.countDown();
}
});
}
});
cb.handler(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception {
// Nothing will be sent.
}
});
Channel sc = null;
BindException bindFailureCause = null;
for (int i = 0; i < 3; i++) {
try {
sc = sb.bind().sync().channel();
break;
} catch (Exception e) {
if (e instanceof BindException) {
logger.warn("Failed to bind to a free port; trying again", e);
bindFailureCause = (BindException) e;
refreshLocalAddress(sb);
} else {
throw e;
}
}
}
if (sc == null) {
throw bindFailureCause;
}
Channel cc;
if (bindClient) {
cc = cb.bind().sync().channel();
} else {
cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
cc = cb.register().sync().channel();
}
for (int i = 0; i < count; i++) {
cc.write(new DatagramPacket(buf.retain().duplicate(), addr));
}
// release as we used buf.retain() before
buf.release();
cc.flush();
assertTrue(latch.await(10, TimeUnit.SECONDS));
sc.close().sync();
cc.close().sync();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.
the class DnsNameResolverContext method parseAddress.
private InetAddress parseAddress(DnsRecord r, String name) {
if (!(r instanceof DnsRawRecord)) {
return null;
}
final ByteBuf content = ((ByteBufHolder) r).content();
final int contentLen = content.readableBytes();
if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
return null;
}
final byte[] addrBytes = new byte[contentLen];
content.getBytes(content.readerIndex(), addrBytes);
try {
return InetAddress.getByAddress(parent.isDecodeIdn() ? IDN.toUnicode(name) : name, addrBytes);
} catch (UnknownHostException e) {
// Should never reach here.
throw new Error(e);
}
}
Aggregations