public UnorderHandlerExecutor(Executor executor) { Assert.assertNotNull(executor, "executor"); this.executor = executor; }
/** * Dumps the given buffer. If the buffer contains only ascii, it will write the buffer content as a String. * * @param buffer The buffer to dump * @return A string representing the buffer content */ public static String dump(ByteBuffer buffer) { return dump(buffer, -1, true); }
@Override public void write(byte[] b, int off, int len) { needSpace(len); buffer.put(b, off, len); }
@Test public void testSingleWrite() throws IOException { ByteBufferOutputStream bbos = new ByteBufferOutputStream(); bbos.write(86); bbos.close(); assertEquals(1, bbos.getByteBuffer().remaining()); assertEquals(86, bbos.getByteBuffer().get()); }
private ByteBuffer serialize(OUT message) { if (message != lastObject) { ByteBufferOutputStream ebbosa = new ByteBufferOutputStream(); ebbosa.setElastic(true); try { ObjectOutputStream oos = new ObjectOutputStream(ebbosa); oos.writeObject(message); oos.close(); lastObject = message; lastSerialized = ebbosa.getByteBuffer(); } catch (IOException e) { throw new IllegalStateException("Serialization exception", e); } } return lastSerialized; }
@Test public void testEmpty() throws IOException { ByteBufferOutputStream bbos = new ByteBufferOutputStream(); bbos.close(); assertEquals(0, bbos.getByteBuffer().remaining()); }
/** * Set the future result of the executing task. Any {@link IoFutureListener}s are notified of the * * @param value the value returned by the executing task. */ protected final void setResult(V value) { assert !isDone(); synchronized (latch) { result.set(value); latch.countDown(); } for (IoFutureListener<V> listener : listeners) { scheduleResult(listener, value); } listeners.clear(); }
@Test public void testEmpty() throws IOException { ByteBuffer bb = ByteBuffer.allocate(0); InputStream is = new ByteBufferInputStream(bb); assertEquals(-1, is.read()); is.close(); }
/** * {@inheritDoc} */ @Override @SuppressWarnings({ "unchecked" }) public V get() throws InterruptedException, ExecutionException { LOG.trace("Entering wait"); latch.await(); LOG.trace("Wait completed"); if (isCancelled()) { throw new CancellationException(); } Object object = result.get(); if (object instanceof ExecutionException) { throw (ExecutionException) object; } else { return (V) object; } }
@Test public void testWrite() throws IOException { byte[] src = "HELLO MINA!".getBytes(); ByteBufferOutputStream bbos = new ByteBufferOutputStream(1024); bbos.write(src); bbos.close(); assertEquals(src.length, bbos.getByteBuffer().remaining()); assertEquals(ByteBuffer.wrap(src), bbos.getByteBuffer()); }
/** * Creates a new instance of ProtocolCodecFilter, with the specified encoder * and decoder. * */ public ProtocolCodecFilter(ProtocolEncoder<MESSAGE, ENCODED, ENCODING_STATE> encoder, ProtocolDecoder<ENCODED, MESSAGE, DECODING_STATE> decoder) { Assert.assertNotNull(encoder, "encoder"); Assert.assertNotNull(decoder, "decoder"); this.encoder = encoder; this.decoder = decoder; }
@Override public void write(int b) { needSpace(1); buffer.put((byte) b); } }
@Test public void testSkip() throws IOException { byte[] src = "HELLO MINA!".getBytes(); ByteBuffer bb = ByteBuffer.wrap(src); InputStream is = new ByteBufferInputStream(bb); is.skip(6); assertEquals(5, is.available()); assertEquals('M', is.read()); assertEquals('I', is.read()); assertEquals('N', is.read()); assertEquals('A', is.read()); is.skip((long) Integer.MAX_VALUE + 1); assertEquals(-1, is.read()); is.close(); }
/** * Returns the value of the user-defined attribute for the given <code>key</code>. * * @param key the attribute's key, must not be <code>null</code> * @return <tt>null</tt> if there is no attribute with the specified key * @exception IllegalArgumentException if <code>key==null</code> * @see #setAttribute(AttributeKey, Object) */ @Override @SuppressWarnings("unchecked") public <T> T getAttribute(AttributeKey<T> key) { assertNotNull(key, "key"); T value = (T) attributes.get(key); return value; }
/** * {@inheritDoc} */ @Override public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) { if (message instanceof ByteBuffer) { log(messageReceivedLevel, "RECEIVED: {}", ByteBufferDumper.dump((ByteBuffer) message)); } else { log(messageReceivedLevel, "RECEIVED: {}", message); } super.messageReceived(session, message, controller); }
/** * Removes the specified Attribute from this container. The old value will be returned, <code>null</code> will be * returned if there is no such attribute in this container.<br> * <br> * This method is equivalent to <code>setAttribute(key,null)</code>. * * @param key of the attribute to be removed,must not be <code>null</code> * @return the removed value, <code>null</code> if this container doesn't contain the specified attribute * @exception IllegalArgumentException if <code>key==null</code> */ @Override @SuppressWarnings("unchecked") public <T> T removeAttribute(AttributeKey<T> key) { assertNotNull(key, "key"); return (T) attributes.remove(key); } }
/** * Returns the value of the user-defined attribute for the given <code>key</code>. * * @param key the attribute's key, must not be <code>null</code> * @return <tt>null</tt> if there is no attribute with the specified key * @exception IllegalArgumentException if <code>key==null</code> * @see #setAttribute(AttributeKey, Object) */ @Override @SuppressWarnings("unchecked") public <T> T getAttribute(AttributeKey<T> key, T defaultValue) { assertNotNull(key, "key"); T value = (T) attributes.get(key); if (value != null) { return value; } return defaultValue; }
/** * Creates a new {@link AttributeKey} with the given parameters. A * {@link IllegalArgumentException} will be thrown if any parameter is * <code>null</code>. * * @param attributeType * type of the referenced attribute-value, must not be * <code>null</code> * @param attributeName * name of this key, must not be <code>null</code> * @exception IllegalArgumentException * if any parameter is <code>null</code> * @see #createKey(Class, String) */ public AttributeKey(Class<T> attributeType, String attributeName) { this.attributeType = assertNotNull(attributeType, "attributeType"); this.attributeName = assertNotNull(attributeName, "attributeName"); this.hashCode = createHashCode(); }
/** * Sets a user-defined attribute. If the <code>value</code> is <code>null</code> the attribute will be removed from * this container. * * @param key the attribute's key, must not be <code>null</code> * @param value the attribute's value, <code>null</code> to remove the attribute * @return The old attribute's value, <code>null</code> if there is no previous value * @exception IllegalArgumentException <ul> * <li>if <code>key==null</code> * <li>if <code>value</code> is not <code>null</code> and not an instance of type that is specified in by * the given <code>key</code> (see {@link AttributeKey#getType()}) * * </ul> * * @see #getAttribute(AttributeKey) */ @Override @SuppressWarnings("unchecked") public <T> T setAttribute(AttributeKey<? extends T> key, T value) { assertNotNull(key, "key"); assertValueIsOfExpectedType(key, value); if (value == null) { return removeAttribute(key); } return (T) attributes.put(key, value); }