List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream
public ByteBufOutputStream(ByteBuf buffer)
From source file:com.allanbank.mongodb.netty.NettyOutputBuffer.java
License:Apache License
/** * Creates a new NettyOutputBuffer.//from w ww . jav a2 s . c om * * @param buffer * The backing {@link ByteBuf}. * @param cache * The cache for encoding strings. */ public NettyOutputBuffer(final ByteBuf buffer, final StringEncoderCache cache) { myBackingBuffer = buffer; myCache = cache; myOutstream = new ByteBufOutputStream(myBackingBuffer); }
From source file:com.allanbank.mongodb.netty.NettyOutputBufferTest.java
License:Apache License
/** * Test method for {@link NettyOutputBuffer#write}. * * @throws IOException//from w w w. j a v a 2s . c o m * On a test failure. */ @Test public void testWrite() throws IOException { final Random rand = new Random(System.currentTimeMillis()); final Message msg = new KillCursors(new long[] { rand.nextLong() }, ReadPreference.PRIMARY); final int msgId = rand.nextInt() & 0xFFFFFF; final ByteBuf expected = ourAllocator.buffer(); final ByteBufOutputStream out = new ByteBufOutputStream(expected); final BsonOutputStream bout = new BsonOutputStream(out); msg.write(msgId, bout); final ByteBuf buffer = ourAllocator.buffer(); final NettyOutputBuffer outBuffer = new NettyOutputBuffer(buffer, new StringEncoderCache()); outBuffer.write(msgId, msg, null); assertThat(buffer, is(expected)); outBuffer.close(); }
From source file:com.barchart.http.server.PooledServerResponse.java
License:BSD License
void init(final ChannelHandlerContext context_, final HttpRequestChannelHandler channelHandler_, final RequestHandler handler_, final PooledServerRequest request_, final RequestLogger logger_) { // Reset default request values if this is a recycled handler if (finished) { headers().clear();/*from ww w . ja v a 2 s.c om*/ content().clear(); setStatus(HttpResponseStatus.OK); } // Reference count increment so underlying ByteBuf is not collected // between requests retain(); context = context_; channelHandler = channelHandler_; handler = handler_; request = request_; logger = logger_; charSet = CharsetUtil.UTF_8; finished = false; suspended = false; started = false; out = new ByteBufOutputStream(content()); writer = new OutputStreamWriter(out, charSet); requestTime = System.currentTimeMillis(); }
From source file:com.barchart.http.server.PooledServerResponse.java
License:BSD License
@Override public void setChunkedEncoding(final boolean chunked) { if (chunked != isChunkedEncoding()) { if (chunked) { HttpHeaders.setTransferEncodingChunked(this); out = new HttpChunkOutputStream(context); writer = new OutputStreamWriter(out, charSet); } else {/*ww w . j av a 2 s . com*/ HttpHeaders.removeTransferEncodingChunked(this); out = new ByteBufOutputStream(content()); writer = new OutputStreamWriter(out, charSet); } } }
From source file:com.chiorichan.factory.groovy.EmbeddedGroovyEngine.java
License:Mozilla Public License
@Override public void setOutput(ByteBuf buffer, Charset charset) { try {/* w w w .ja v a 2s. c o m*/ binding.setProperty("out", new PrintStream(new ByteBufOutputStream(buffer), true, charset.name())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:com.chiorichan.factory.groovy.NestedScript.java
License:Mozilla Public License
@SuppressWarnings("unchecked") public NestedScript() { HttpRequestWrapper request = HttpRequestWrapper.getRequest(); Binding binding = new Binding(HttpRequestWrapper.getRequest().getBinding().getVariables()); try {//from w w w . ja v a 2 s .co m binding.setProperty("out", new PrintStream(new ByteBufOutputStream(request.getEvalFactory().getOutputStream()), true, request.getEvalFactory().getCharset().name())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // setBinding( binding ); getBinding().getVariables().putAll(binding.getVariables()); // Setting the binding directly was causing an override of variable value from last class instance. // Possibly a link to the groovy MetaClass, hmm, thoughts on how to make use of this... }
From source file:com.codeabovelab.dm.platform.http.async.NettyRequest.java
License:Apache License
NettyRequest(Bootstrap bootstrap, URI uri, HttpMethod method) { this.bootstrap = bootstrap; this.uri = uri; this.method = method; this.body = new ByteBufOutputStream(Unpooled.buffer(1024)); }
From source file:com.corundumstudio.socketio.parser.Encoder.java
License:Apache License
public int encodePacket(Packet packet, ByteBuf buffer) throws IOException { ByteBufOutputStream out = new ByteBufOutputStream(buffer); int start = buffer.writerIndex(); int type = packet.getType().getValue(); buffer.writeByte(toChar(type));/*from w w w.j ava 2 s .c om*/ buffer.writeByte(Packet.SEPARATOR); Long id = packet.getId(); String endpoint = packet.getEndpoint(); Object ack = packet.getAck(); if (Packet.ACK_DATA.equals(ack)) { buffer.writeBytes(toChars(id)); buffer.writeByte('+'); } else { if (id != null) { buffer.writeBytes(toChars(id)); } } buffer.writeByte(Packet.SEPARATOR); if (endpoint != null) { buffer.writeBytes(endpoint.getBytes()); } switch (packet.getType()) { case MESSAGE: if (packet.getData() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] data = packet.getData().toString().getBytes(); buffer.writeBytes(data); } break; case EVENT: List<Object> args = packet.getArgs(); if (args.isEmpty()) { args = null; } buffer.writeByte(Packet.SEPARATOR); Event event = new Event(packet.getName(), args); jsonSupport.writeValue(out, event); break; case JSON: buffer.writeByte(Packet.SEPARATOR); jsonSupport.writeValue(out, packet.getData()); break; case CONNECT: if (packet.getQs() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] qsData = packet.getQs().toString().getBytes(); buffer.writeBytes(qsData); } break; case ACK: if (packet.getAckId() != null || !packet.getArgs().isEmpty()) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getAckId() != null) { byte[] ackIdData = toChars(packet.getAckId()); buffer.writeBytes(ackIdData); } if (!packet.getArgs().isEmpty()) { buffer.writeByte('+'); jsonSupport.writeValue(out, packet.getArgs()); } break; case ERROR: if (packet.getReason() != null || packet.getAdvice() != null) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getReason() != null) { int reasonCode = packet.getReason().getValue(); buffer.writeByte(toChar(reasonCode)); } if (packet.getAdvice() != null) { int adviceCode = packet.getAdvice().getValue(); buffer.writeByte('+'); buffer.writeByte(toChar(adviceCode)); } break; } return charsScanner.getLength(buffer, start); }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary) throws IOException { ByteBuf buf = buffer;/* w ww . j av a 2 s. c o m*/ if (!binary) { buf = allocateBuffer(allocator); } byte type = toChar(packet.getType().getValue()); buf.writeByte(type); try { switch (packet.getType()) { case PONG: { buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8)); break; } case OPEN: { ByteBufOutputStream out = new ByteBufOutputStream(buf); jsonSupport.writeValue(out, packet.getData()); break; } case MESSAGE: { ByteBuf encBuf = null; if (packet.getSubType() == PacketType.ERROR) { encBuf = allocateBuffer(allocator); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, packet.getData()); } if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) { List<Object> values = new ArrayList<Object>(); if (packet.getSubType() == PacketType.EVENT) { values.add(packet.getName()); } encBuf = allocateBuffer(allocator); List<Object> args = packet.getData(); values.addAll(args); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, values); if (!jsonSupport.getArrays().isEmpty()) { packet.initAttachments(jsonSupport.getArrays().size()); for (byte[] array : jsonSupport.getArrays()) { packet.addAttachment(Unpooled.wrappedBuffer(array)); } packet.setSubType(PacketType.BINARY_EVENT); } } byte subType = toChar(packet.getSubType().getValue()); buf.writeByte(subType); if (packet.hasAttachments()) { byte[] ackId = toChars(packet.getAttachments().size()); buf.writeBytes(ackId); buf.writeByte('-'); } if (packet.getSubType() == PacketType.CONNECT) { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); } } else { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); buf.writeByte(','); } } if (packet.getAckId() != null) { byte[] ackId = toChars(packet.getAckId()); buf.writeBytes(ackId); } if (encBuf != null) { buf.writeBytes(encBuf); encBuf.release(); } break; } } } finally { // we need to write a buffer in any case if (!binary) { buffer.writeByte(0); int length = buf.writerIndex(); buffer.writeBytes(longToBytes(length)); buffer.writeByte(0xff); buffer.writeBytes(buf); buf.release(); } } }
From source file:com.cu.http.container.core.adaptor.NettyServletOutputStream.java
License:Apache License
public NettyServletOutputStream(HttpContent httpContent) { this.out = new ByteBufOutputStream(httpContent.content()); }