private final SendBuffer acquire(SctpPayload message) { final ChannelBuffer src = message.getPayloadBuffer(); final int streamNo = message.getStreamIdentifier(); final int protocolId = message.getProtocolIdentifier(); final int size = src.readableBytes(); if (size == 0) { return EMPTY_BUFFER; } if (src.isDirect()) { return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer()); } if (src.readableBytes() > DEFAULT_PREALLOCATION_SIZE) { return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer()); } Preallocation current = this.current; ByteBuffer buffer = current.buffer; int remaining = buffer.remaining(); PooledSendBuffer dst; if (size < remaining) { int nextPos = buffer.position() + size; ByteBuffer slice = buffer.duplicate(); buffer.position(align(nextPos)); slice.limit(nextPos); current.refCnt++; dst = new PooledSendBuffer(streamNo, protocolId, current, slice); } else if (size > remaining) { this.current = current = getPreallocation(); buffer = current.buffer; ByteBuffer slice = buffer.duplicate(); buffer.position(align(size)); slice.limit(size); current.refCnt++; dst = new PooledSendBuffer(streamNo, protocolId, current, slice); } else { // size == remaining current.refCnt++; this.current = getPreallocation0(); dst = new PooledSendBuffer(streamNo, protocolId, current, current.buffer); } ByteBuffer dstbuf = dst.buffer; dstbuf.mark(); src.getBytes(src.readerIndex(), dstbuf); dstbuf.reset(); return dst; }
public static HttpRequest createSendDataRequest(String host, String cookie, ChannelBuffer data) { HttpRequest request = createRequestTemplate(host, cookie, CLIENT_SEND_REQUEST_URI); request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Long.toString(data.readableBytes())); request.setContent(data); return request; }
@Override public Object decode(ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in) throws Exception { if (in.readableBytes() < frameLength) { return null; } else { return in.readBytes(frameLength); } }
public static HttpResponse createRejection(HttpRequest request, String reason) { HttpVersion version = request != null ? request.getProtocolVersion() : HttpVersion.HTTP_1_1; HttpResponse response = new DefaultHttpResponse(version, HttpResponseStatus.BAD_REQUEST); response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=\"utf-8\""); ChannelBuffer reasonBuffer = ChannelBuffers.wrappedBuffer(toBytes(reason)); response.setHeader( HttpHeaders.Names.CONTENT_LENGTH, Integer.toString(reasonBuffer.readableBytes())); response.setContent(reasonBuffer); return response; }
private static HttpResponse createResponseTemplate( HttpResponseStatus status, ChannelBuffer data) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); if (data != null) { response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Integer.toString(data.readableBytes())); response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream"); response.setContent(data); } else { response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0"); response.setContent(null); } return response; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (server) { Channels.write(channel, e.getMessage(), e.getRemoteAddress()); } else { ChannelBuffer m = (ChannelBuffer) e.getMessage(); byte[] actual = new byte[m.readableBytes()]; m.getBytes(0, actual); int lastIdx = counter; for (int i = 0; i < actual.length; i++) { assertEquals(frames.getByte(ignoredBytes + i + lastIdx), actual[i]); } counter += actual.length; } }
@Override public void setContent(ChannelBuffer buffer) throws IOException { if (buffer.readableBytes() > limitSize) { if (fileUpload instanceof MemoryFileUpload) { // change to Disk fileUpload = new DiskFileUpload( fileUpload.getName(), fileUpload.getFilename(), fileUpload.getContentType(), fileUpload.getContentTransferEncoding(), fileUpload.getCharset(), definedSize); } } fileUpload.setContent(buffer); }
@Override public void addContent(ChannelBuffer buffer, boolean last) throws IOException { if (fileUpload instanceof MemoryFileUpload) { if (fileUpload.length() + buffer.readableBytes() > limitSize) { DiskFileUpload diskFileUpload = new DiskFileUpload( fileUpload.getName(), fileUpload.getFilename(), fileUpload.getContentType(), fileUpload.getContentTransferEncoding(), fileUpload.getCharset(), definedSize); if (((MemoryFileUpload) fileUpload).getChannelBuffer() != null) { diskFileUpload.addContent(((MemoryFileUpload) fileUpload).getChannelBuffer(), last); } fileUpload = diskFileUpload; } } fileUpload.addContent(buffer, last); }
@Override public void setInput(ChannelBuffer decompressed) { byte[] in = new byte[decompressed.readableBytes()]; decompressed.readBytes(in); compressor.setInput(in); }