/** * Creates a new embedder whose pipeline is composed of the specified * handlers. */ protected AbstractCodecEmbedder(ChannelHandler... handlers) { pipeline = new EmbeddedChannelPipeline(); configurePipeline(handlers); channel = new EmbeddedChannel(pipeline, sink); fireInitialEvents(); }
private ChannelBuffer finishEncode() { if (encoder == null) { offerred = false; return ChannelBuffers.EMPTY_BUFFER; } ChannelBuffer result; if (!offerred) { // No data was offerred to the encoder since the encoder was created. // We should offer at least an empty buffer so that the encoder knows its is encoding empty content. offerred = false; encoder.offer(ChannelBuffers.EMPTY_BUFFER); } if (encoder.finish()) { result = ChannelBuffers.wrappedBuffer(encoder.pollAll(new ChannelBuffer[encoder.size()])); } else { result = ChannelBuffers.EMPTY_BUFFER; } encoder = null; return result; }
/** * Creates a new instance with the specified parameters. */ protected RtspMessageDecoder(int maxInitialLineLength, int maxHeaderSize, int maxContentLength) { super(maxInitialLineLength, maxHeaderSize, maxContentLength * 2); aggregator = new DecoderEmbedder<HttpMessage>(new HttpChunkAggregator(maxContentLength)); }
private void startNegotiation(boolean fakeLoopback) { Negotiator negotiator = new Negotiator("127.0.0.1", secContext, false); negotiator.overrideLoopbackForTests = fakeLoopback; embedder = new DecoderEmbedder<Object>(negotiator); negotiator.sendHello(embedder.getPipeline().getChannel()); }
private ChannelBuffer finishDecode() { if (decoder == null) { return ChannelBuffers.EMPTY_BUFFER; } ChannelBuffer result; if (decoder.finish()) { result = ChannelBuffers.wrappedBuffer(decoder.pollAll(new ChannelBuffer[decoder.size()])); } else { result = ChannelBuffers.EMPTY_BUFFER; } decoder = null; return result; }
@Override protected void notifyHandlerException(ChannelEvent e, Throwable t) { while (t instanceof ChannelPipelineException && t.getCause() != null) { t = t.getCause(); } if (t instanceof CodecEmbedderException) { throw (CodecEmbedderException) t; } else { throw new CodecEmbedderException(t); } } }
@Override protected EncoderEmbedder<ChannelBuffer> newContentEncoder( HttpMessage msg, String acceptEncoding) throws Exception { String contentEncoding = msg.headers().get(HttpHeaders.Names.CONTENT_ENCODING); if (contentEncoding != null && !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) { // Encoded already. return null; } ZlibWrapper wrapper = determineWrapper(acceptEncoding); if (wrapper == null) { return null; } if (DetectionUtil.javaVersion() < 7 || noJdkZlibEncoder || windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) { return new EncoderEmbedder<ChannelBuffer>( new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)); } else { return new EncoderEmbedder<ChannelBuffer>( new JdkZlibEncoder(wrapper, compressionLevel)); } }
public boolean offer(Object input) { fireMessageReceived(getChannel(), input); return !isEmpty(); } }
public boolean offer(Object input) { write(getChannel(), input).setSuccess(); return !isEmpty(); } }
/** * Creates a new embedder whose pipeline is composed of the specified * handlers. * * @param bufferFactory the {@link ChannelBufferFactory} to be used when * creating a new buffer. */ protected AbstractCodecEmbedder(ChannelBufferFactory bufferFactory, ChannelHandler... handlers) { this(handlers); getChannel().getConfig().setBufferFactory(bufferFactory); }
@Override protected DecoderEmbedder<ChannelBuffer> newContentDecoder(String contentEncoding) throws Exception { if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) { return new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.GZIP)); } if ("deflate".equalsIgnoreCase(contentEncoding) || "x-deflate".equalsIgnoreCase(contentEncoding)) { // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.ZLIB_OR_NONE)); } // 'identity' or unsupported return null; } }
@Override protected void notifyHandlerException(ChannelEvent e, Throwable t) { while (t instanceof ChannelPipelineException && t.getCause() != null) { t = t.getCause(); } if (t instanceof CodecEmbedderException) { throw (CodecEmbedderException) t; } else { throw new CodecEmbedderException( t ); } } }
@SuppressWarnings("unchecked") public final <T> T[] pollAll(T[] a) { if (a == null) { throw new NullPointerException("a"); } final int size = size(); // Create a new array if the specified one is too small. if (a.length < size) { a = (T[]) Array.newInstance(a.getClass().getComponentType(), size); } for (int i = 0;; i ++) { T product = (T) poll(); if (product == null) { break; } a[i] = product; } // Put the terminator if necessary. if (a.length > size) { a[size] = null; } return a; }
@Before public void setUp() { this.encoder = new EncoderEmbedder<StompContentFrame>( new StompMessageEncoder() ); }
public void exceptionCaught( ChannelPipeline pipeline, ChannelEvent e, ChannelPipelineException cause) throws Exception { Throwable actualCause = cause.getCause(); if (actualCause == null) { actualCause = cause; } throw new CodecEmbedderException( actualCause ); }