/** * Creates a new text frame from with the specified string. */ public WebSocketFrameImpl(String textData, boolean isFinalFrame) { this.type = FrameType.TEXT; this.isFinalFrame = isFinalFrame; this.binaryData = Unpooled.copiedBuffer(textData, CharsetUtil.UTF_8); }
/** * Creates a new big-endian buffer whose content is the specified * {@code array} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer(char[] array, Charset charset) { if (array == null) { throw new NullPointerException("array"); } return copiedBuffer(array, 0, array.length, charset); }
/** * Creates a new big-endian buffer whose content is the specified * {@code string} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer(CharSequence string, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (string instanceof CharBuffer) { return copiedBuffer((CharBuffer) string, charset); } return copiedBuffer(CharBuffer.wrap(string), charset); }
public void setTextData(String textData) { if (this.binaryData != null) { this.binaryData.release(); } this.binaryData = Unpooled.copiedBuffer(textData, CharsetUtil.UTF_8); }
/** * Creates a new big-endian buffer whose content is the specified * {@code array} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer(char[] array, Charset charset) { if (array == null) { throw new NullPointerException("array"); } return copiedBuffer(array, 0, array.length, charset); }
/** * Creates a new big-endian buffer whose content is a subregion of * the specified {@code array} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer(char[] array, int offset, int length, Charset charset) { if (array == null) { throw new NullPointerException("array"); } if (length == 0) { return EMPTY_BUFFER; } return copiedBuffer(CharBuffer.wrap(array, offset, length), charset); }
private void sendServiceUnavailable(Channel ch) { ch.writeAndFlush( Unpooled.copiedBuffer("HTTP/1.1 503 Service Unavailable\r\n" + "Content-Length:0\r\n" + "\r\n", StandardCharsets.ISO_8859_1)) .addListener(ChannelFutureListener.CLOSE); }
public static ByteBuf generateWSCloseFrameByteBuf(short statusCode, String reason) { if (reason != null) return Unpooled.copiedBuffer( Unpooled.copyShort(statusCode), // First two bytes are reserved for status code Unpooled.copiedBuffer(reason, Charset.forName("UTF-8")) ); else return Unpooled.copyShort(statusCode); }
@Override public NetSocket write(String str) { write(Unpooled.copiedBuffer(str, CharsetUtil.UTF_8)); return this; }
@Override public NetSocket write(String str, String enc) { synchronized (conn) { Charset cs = enc != null ? Charset.forName(enc) : CharsetUtil.UTF_8; writeData(Unpooled.copiedBuffer(str, cs), false); return this; } }
public void write(byte[]... buf) throws IOException { if (channel != null && channel.isWritable()) { channel.writeAndFlush(Unpooled.copiedBuffer(buf)); } else { throw new IOException("write failed ! please checking !"); } }
@Override public NetSocket write(String str, String enc) { if (enc == null) { write(str); } else { write(Unpooled.copiedBuffer(str, Charset.forName(enc))); } return this; }
private static ByteBuf paddedByteBuf(int padding, byte[] bytes) { byte[] data = new byte[padding + bytes.length]; System.arraycopy(bytes, 0, data, padding, bytes.length); return Unpooled.copiedBuffer(data).slice(padding, bytes.length); }
@Test public void testBufferBody3GetBody() { final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers()); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes()))); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("World!".getBytes()))); msg.bufferBodyContents(new DefaultLastHttpContent()); final String body = new String(msg.getBody()); assertTrue(msg.hasBody()); assertTrue(msg.hasCompleteBody()); assertEquals("Hello World!", body); }
@Test public void testAppendDoesNotModifyByteBufIndex() throws Exception { ByteBuf buf = Unpooled.copiedBuffer("foobar".getBytes()); assertEquals(0, buf.readerIndex()); assertEquals(6, buf.writerIndex()); Buffer buffer = Buffer.buffer(buf); Buffer other = Buffer.buffer("prefix"); other.appendBuffer(buffer); assertEquals(0, buf.readerIndex()); assertEquals(6, buf.writerIndex()); assertEquals(other.toString(), "prefixfoobar"); }
@Test public void testBufferBody3GetBodyAsText() { final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers()); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes()))); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("World!".getBytes()))); msg.bufferBodyContents(new DefaultLastHttpContent()); final String body = msg.getBodyAsText(); assertTrue(msg.hasBody()); assertTrue(msg.hasCompleteBody()); assertEquals("Hello World!", body); }
@Override public void setBodyAsText(String bodyText) { disposeBufferedBody(); if (! Strings.isNullOrEmpty(bodyText)) { final ByteBuf content = Unpooled.copiedBuffer(bodyText.getBytes(Charsets.UTF_8)); bufferBodyContents(new DefaultLastHttpContent(content)); setContentLength(bodyText.getBytes(CS_UTF8).length); } else { bufferBodyContents(new DefaultLastHttpContent()); setContentLength(0); } }
@Test public void testLength2() throws Exception { byte[] bytes = TestUtils.randomByteArray(100); assertEquals(90, Buffer.buffer(Unpooled.copiedBuffer(bytes).slice(10, 90)).length()); }
@Test public void testBufferBody2GetBody() { final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers()); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes()))); msg.bufferBodyContents(new DefaultLastHttpContent(Unpooled.copiedBuffer("World!".getBytes()))); final String body = new String(msg.getBody()); assertTrue(msg.hasBody()); assertTrue(msg.hasCompleteBody()); assertEquals("Hello World!", body); }
@Test public void testBufferBody3GetBodyAsText() { final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers()); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("Hello ".getBytes()))); msg.bufferBodyContents(new DefaultHttpContent(Unpooled.copiedBuffer("World!".getBytes()))); msg.bufferBodyContents(new DefaultLastHttpContent()); final String body = msg.getBodyAsText(); assertTrue(msg.hasBody()); assertTrue(msg.hasCompleteBody()); assertEquals("Hello World!", body); }