/** * Creates a new big-endian buffer whose content is a copy of the * specified {@code array}'s sub-region. The new buffer's * {@code readerIndex} and {@code writerIndex} are {@code 0} and * the specified {@code length} respectively. */ public static ChannelBuffer copiedBuffer(byte[] array, int offset, int length) { return copiedBuffer(BIG_ENDIAN, array, offset, length); }
/** * 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 ChannelBuffer copiedBuffer(char[] array, Charset charset) { return copiedBuffer(BIG_ENDIAN, array, 0, array.length, charset); }
/** * Creates a new big-endian buffer whose content is a subregion of * 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 ChannelBuffer copiedBuffer( CharSequence string, int offset, int length, Charset charset) { return copiedBuffer(BIG_ENDIAN, string, offset, 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 ChannelBuffer copiedBuffer( char[] array, int offset, int length, Charset charset) { return copiedBuffer(BIG_ENDIAN, array, offset, length, charset); }
/** * Creates a new buffer with the specified {@code endianness} 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 ChannelBuffer copiedBuffer(ByteOrder endianness, char[] array, Charset charset) { return copiedBuffer(endianness, array, 0, array.length, charset); }
/** * Creates a new big-endian buffer whose content is a copy of the * specified {@code array}. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0} and {@code array.length} respectively. */ public static ChannelBuffer copiedBuffer(byte[] array) { return copiedBuffer(BIG_ENDIAN, array); }
/** * Creates a new big-endian buffer whose content is a merged copy of * the specified {@code arrays}. The new buffer's {@code readerIndex} * and {@code writerIndex} are {@code 0} and the sum of all arrays' * {@code length} respectively. */ public static ChannelBuffer copiedBuffer(byte[]... arrays) { return copiedBuffer(BIG_ENDIAN, arrays); }
/** * 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 ChannelBuffer copiedBuffer(CharSequence string, Charset charset) { return copiedBuffer(BIG_ENDIAN, string, charset); }
public ChannelBuffer getPolicyFileContents() throws Exception { return ChannelBuffers.copiedBuffer( "<?xml version=\"1.0\"?>" + NEWLINE + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">" + NEWLINE + "" + NEWLINE + "<!-- Policy file for xmlsocket://socks.example.com -->" + NEWLINE + "<cross-domain-policy> " + NEWLINE + "" + NEWLINE + " <!-- This is a master socket policy file -->" + NEWLINE + " <!-- No other socket policies on the host will be permitted -->" + NEWLINE + " <site-control permitted-cross-domain-policies=\"master-only\"/>" + NEWLINE + "" + NEWLINE + " <!-- Instead of setting to-ports=\"*\", administrator's can use ranges and commas -->" + NEWLINE + " <allow-access-from domain=\"*\" to-ports=\"" + portNumber + "\" />" + NEWLINE + "" + NEWLINE + "</cross-domain-policy>" + NEWLINE, CharsetUtil.US_ASCII); }
/** * Creates a new buffer with the specified {@code endianness} 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 ChannelBuffer copiedBuffer(ByteOrder endianness, CharSequence string, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (string instanceof CharBuffer) { return copiedBuffer(endianness, (CharBuffer) string, charset); } return copiedBuffer(endianness, CharBuffer.wrap(string), charset); }
/** * Creates a new buffer with the specified {@code endianness} 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 ChannelBuffer copiedBuffer( ByteOrder endianness, char[] array, int offset, int length, Charset charset) { if (array == null) { throw new NullPointerException("array"); } if (length == 0) { return EMPTY_BUFFER; } return copiedBuffer( endianness, CharBuffer.wrap(array, offset, length), charset); }
public ChannelBuffer toChannelBuffer() { ChannelBuffer[] buffers = new ChannelBuffer[value.size()]; for (int i = 0; i < buffers.length; i++) { buffers[i] = ChannelBuffers.copiedBuffer(value.get(i), charset); } return ChannelBuffers.wrappedBuffer(buffers); }
/** * Sets the string for this frame * * @param text * text to store */ public void setText(String text) { if (text == null) { throw new NullPointerException("text"); } setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8)); }
/** * Sets the string for this frame * * @param text * text to store */ public void setText(String text) { if (text == null || text.length() == 0) { setBinaryData(ChannelBuffers.EMPTY_BUFFER); } else { setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8)); } }
/** * Creates a new text frame with the specified text string. The final fragment flag is set to true. * * @param text * String to put in the frame */ public TextWebSocketFrame(String text) { if (text == null || text.length() == 0) { setBinaryData(ChannelBuffers.EMPTY_BUFFER); } else { setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8)); } }
static ChannelBuffer readPrivateKey(File file) throws KeyException { String content; try { content = readContent(file); } catch (IOException e) { throw new KeyException("failed to read a file: " + file, e); } Matcher m = KEY_PATTERN.matcher(content); if (!m.find()) { throw new KeyException("found no private key: " + file); } return Base64.decode(ChannelBuffers.copiedBuffer(m.group(1), CharsetUtil.US_ASCII)); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg instanceof String) { msg = ChannelBuffers.copiedBuffer((String) msg, CharsetUtil.US_ASCII); } else if (!(msg instanceof ChannelBuffer)) { return msg; } ChannelBuffer src = (ChannelBuffer) msg; return Base64.decode( src, src.readerIndex(), src.readableBytes(), dialect); }
@Override protected Object encode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg instanceof String) { return copiedBuffer( ctx.getChannel().getConfig().getBufferFactory().getDefaultOrder(), (String) msg, charset); } return msg; } }
protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.headers().add(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Put shuffle version into http header response.headers().add(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME); response.headers().add(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION); response.setContent( ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
/** * Writes all error responses to the client. * * TODO REST-Server 1. collect error stats * * @param messageEvent - for retrieving the channel details * @param status - error code * @param message - error message */ public static void writeErrorResponse(MessageEvent messageEvent, HttpResponseStatus status, String message) { // Create the Response object HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + ". " + message + "\r\n", CharsetUtil.UTF_8)); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); // Write the response to the Netty Channel messageEvent.getChannel().write(response); } }