List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:dorkbox.network.pipeline.tcp.KryoDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception { // Make sure if the length field was received, // and read the length of the next object from the socket. int lengthLength = OptimizeUtilsByteBuf.canReadInt(in); int readableBytes = in.readableBytes(); // full length of available bytes. if (lengthLength == 0 || readableBytes < 2 || readableBytes < lengthLength) { // The length field was not fully received - do nothing (wait for more...) // This method will be invoked again when more packets are // received and appended to the buffer. return;//from www .j a v a 2 s . c o m } // The length field is in the buffer. // save the writerIndex for local access int writerIndex = in.writerIndex(); // Mark the current buffer position before reading the length fields // because the whole frame might not be in the buffer yet. // We will reset the buffer position to the marked position if // there's not enough bytes in the buffer. in.markReaderIndex(); // Read the length field. int length = OptimizeUtilsByteBuf.readInt(in, true); readableBytes = in.readableBytes(); // have to adjust readable bytes, since we just read an int off the buffer. if (length == 0) { context.fireExceptionCaught(new IllegalStateException("KryoDecoder had a read length of 0")); return; } // we can't test against a single "max size", since objects can back-up on the buffer. // we must ABSOLUTELY follow a "max size" rule when encoding objects, however. final NetworkSerializationManager serializationManager = this.serializationManager; // Make sure if there's enough bytes in the buffer. if (length > readableBytes) { // The whole bytes were not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. // Reset to the marked position to read the length field again // next time. in.resetReaderIndex(); // wait for the rest of the object to come in. // System.err.println(Thread.currentThread().getName() + " waiting for more of the object to arrive"); } // how many objects are on this buffer? else if (readableBytes > length) { // more than one! // read the object off of the buffer. (save parts of the buffer so if it is too big, we can go back to it, and use it later on...) // we know we have at least one object int objectCount = 1; int endOfObjectPosition = in.readerIndex() + length; // set us up for the next object. in.readerIndex(endOfObjectPosition); // how many more objects?? The first time, it can be off, because we already KNOW it's > 0. // (That's how we got here to begin with) while (readableBytes > 0) { if (OptimizeUtilsByteBuf.canReadInt(in) > 0) { length = OptimizeUtilsByteBuf.readInt(in, true); if (length <= 0) { // throw new IllegalStateException("Kryo DecoderTCP had a read length of 0"); break; } endOfObjectPosition = in.readerIndex() + length; // position the reader to look for the NEXT object if (endOfObjectPosition <= writerIndex) { in.readerIndex(endOfObjectPosition); readableBytes = in.readableBytes(); objectCount++; } else { break; } } else { break; } } // readerIndex is currently at the MAX place it can read data off the buffer. // reset it to the spot BEFORE we started reading data from the buffer. in.resetReaderIndex(); // System.err.println("Found " + objectCount + " objects in this buffer."); // NOW add each one of the NEW objects to the array! for (int i = 0; i < objectCount; i++) { length = OptimizeUtilsByteBuf.readInt(in, true); // object LENGTH // however many we need to Object object; try { object = readObject(serializationManager, context, in, length); out.add(object); } catch (Exception ex) { context.fireExceptionCaught(new IOException("Unable to deserialize object!", ex)); } } // the buffer reader index will be at the correct location, since the read object method advances it. } else { // exactly one! Object object; try { object = readObject(serializationManager, context, in, length); out.add(object); } catch (Exception ex) { context.fireExceptionCaught( new IOException("Unable to deserialize object for " + this.getClass(), ex)); } } }
From source file:dorkbox.network.pipeline.tcp.KryoEncoder.java
License:Apache License
@Override protected void encode(final ChannelHandlerContext context, final Object msg, final ByteBuf out) throws Exception { // we don't necessarily start at 0!! // START at index = 4. This is to make room for the integer placed by the frameEncoder for TCP. int startIndex = out.writerIndex() + reservedLengthIndex; if (msg != null) { out.writerIndex(startIndex);/*from ww w . j a v a2 s . c om*/ try { writeObject(this.serializationManager, context, msg, out); int index = out.writerIndex(); // now set the frame length // (reservedLengthLength) 4 is the reserved space for the integer. int length = index - startIndex; // specify the header. int lengthOfTheLength = OptimizeUtilsByteBuf.intLength(length, true); // make it so the location we write out our length is aligned to the end. int indexForLength = startIndex - lengthOfTheLength; out.writerIndex(indexForLength); // do the optimized length thing! OptimizeUtilsByteBuf.writeInt(out, length, true); // newIndex is actually where we want to start reading the data as well when written to the socket out.setIndex(indexForLength, index); } catch (Exception ex) { context.fireExceptionCaught( new IOException("Unable to serialize object of type: " + msg.getClass().getName(), ex)); } } }
From source file:dorkbox.network.pipeline.udp.KryoDecoderUdp.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, Object message, List<Object> out) throws Exception { ByteBuf data = (ByteBuf) ((AddressedEnvelope) message).content(); try {// w w w. j a va 2s.c o m // no connection here because we haven't created one yet. When we do, we replace this handler with a new one. Object object = serializationManager.read(data, data.writerIndex()); out.add(object); } catch (IOException e) { String msg = "Unable to deserialize object"; LoggerFactory.getLogger(this.getClass()).error(msg, e); throw new IOException(msg, e); } }
From source file:dorkbox.network.serialization.Serialization.java
License:Apache License
/** * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize. * <p>//w w w. ja v a2 s. c o m * No crypto and no sequence number * <p> * There is a small speed penalty if there were no kryo's available to use. */ @Override public final void write(final ByteBuf buffer, final Object message) throws IOException { final KryoExtra kryo = kryoPool.take(); try { if (wireWriteLogger.isTraceEnabled()) { int start = buffer.writerIndex(); kryo.write(buffer, message); int end = buffer.writerIndex(); wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start)); } else { kryo.write(buffer, message); } } finally { kryoPool.put(kryo); } }
From source file:dorkbox.network.serialization.Serialization.java
License:Apache License
/** * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize. * <p>/* www .ja v a2s . c om*/ * There is a small speed penalty if there were no kryo's available to use. */ @Override public final void writeWithCrypto(final Connection_ connection, final ByteBuf buffer, final Object message) throws IOException { final KryoExtra kryo = kryoPool.take(); try { // we only need to encrypt when NOT on loopback, since encrypting on loopback is a waste of CPU if (connection.isLoopback()) { if (wireWriteLogger.isTraceEnabled()) { int start = buffer.writerIndex(); kryo.writeCompressed(connection, buffer, message); int end = buffer.writerIndex(); wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start)); } else { kryo.writeCompressed(connection, buffer, message); } } else { if (wireWriteLogger.isTraceEnabled()) { int start = buffer.writerIndex(); kryo.writeCrypto(connection, buffer, message); int end = buffer.writerIndex(); wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start)); } else { kryo.writeCrypto(connection, buffer, message); } } } finally { kryoPool.put(kryo); } }
From source file:eu.jangos.realm.network.encoder.RealmPacketEncoder.java
License:Open Source License
private void reverseHeader(ChannelHandlerContext ctx, ByteBuf out) { // Retaining the actual index. int index = out.writerIndex(); out.resetReaderIndex();/*from w ww . j av a 2 s . co m*/ // Reading the header. byte[] header = out.readBytes(4).array(); // Resetting the reader index to send it. out.resetReaderIndex(); // Swapping the header. byte temp = header[2]; header[2] = header[3]; header[3] = temp; // Encrypting the header (if applicable). header = ctx.channel().attr(CRYPT).get().encrypt(header); // Reset the writer index. out.resetWriterIndex(); // Write the header. out.writeBytes(header); // Reset the writer index to the default one. out.writerIndex(index); }
From source file:eu.stratosphere.runtime.io.network.netty.OutboundEnvelopeEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Envelope env, ByteBuf out) throws Exception { // -------------------------------------------------------------------- // (1) header (48 bytes) // -------------------------------------------------------------------- out.writeInt(MAGIC_NUMBER); // 4 bytes if (out.getInt(out.writerIndex() - 4) != MAGIC_NUMBER) { throw new RuntimeException(); }//from ww w . j a v a2 s .c om out.writeInt(env.getSequenceNumber()); // 4 bytes env.getJobID().writeTo(out); // 16 bytes env.getSource().writeTo(out); // 16 bytes out.writeInt(env.getEventsSerialized() != null ? env.getEventsSerialized().remaining() : 0); // 4 bytes out.writeInt(env.getBuffer() != null ? env.getBuffer().size() : 0); // 4 bytes // -------------------------------------------------------------------- // (2) events (var length) // -------------------------------------------------------------------- if (env.getEventsSerialized() != null) { out.writeBytes(env.getEventsSerialized()); } // -------------------------------------------------------------------- // (3) buffer (var length) // -------------------------------------------------------------------- if (env.getBuffer() != null) { Buffer buffer = env.getBuffer(); out.writeBytes(buffer.getMemorySegment().wrap(0, buffer.size())); // Recycle the buffer from OUR buffer pool after everything has been // copied to Nettys buffer space. buffer.recycleBuffer(); } }
From source file:hivemall.mix.MixMessageEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, MixMessage msg, ByteBuf out) throws Exception { int startIdx = out.writerIndex(); out.writeBytes(LENGTH_PLACEHOLDER);/*w w w .j a va 2 s . com*/ MixEventName event = msg.getEvent(); byte b = event.getID(); out.writeByte(b); Object feature = msg.getFeature(); encodeObject(feature, out); float weight = msg.getWeight(); out.writeFloat(weight); float covariance = msg.getCovariance(); out.writeFloat(covariance); short clock = msg.getClock(); out.writeShort(clock); int deltaUpdates = msg.getDeltaUpdates(); out.writeInt(deltaUpdates); boolean cancelRequest = msg.isCancelRequest(); out.writeBoolean(cancelRequest); String groupId = msg.getGroupID(); writeString(groupId, out); int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); }
From source file:impl.underdark.transport.bluetooth.BtLink.java
License:Open Source License
private void inputLoop() { // Input I/O thread. sendHelloFrame();/* w w w . j av a 2 s . com*/ int bufferSize = 4096; ByteBuf inputData = Unpooled.buffer(bufferSize); inputData.order(ByteOrder.BIG_ENDIAN); try { int len; while (true) { inputData.ensureWritable(bufferSize, true); len = inputStream.read(inputData.array(), inputData.writerIndex(), bufferSize); if (len <= 0) break; inputData.writerIndex(inputData.writerIndex() + len); if (!formFrames(inputData)) break; inputData.discardReadBytes(); inputData.capacity(inputData.writerIndex() + bufferSize); } // while } catch (InterruptedIOException ex) { Logger.warn("bt input timeout: {}", ex); try { inputStream.close(); } catch (IOException ioex) { } notifyDisconnect(); return; } catch (Exception ex) { Logger.warn("bt input read failed.", ex); try { inputStream.close(); } catch (IOException ioex) { } notifyDisconnect(); return; } Logger.debug("bt input read end."); notifyDisconnect(); }
From source file:impl.underdark.transport.nsd.NsdLink.java
License:Open Source License
private void inputLoop() { // Input thread. final int bufferSize = 4096; ByteBuf inputData = Unpooled.buffer(bufferSize); inputData.order(ByteOrder.BIG_ENDIAN); try {/*w ww . jav a 2 s. co m*/ int len; while (true) { inputData.ensureWritable(bufferSize, true); len = inputStream.read(inputData.array(), inputData.writerIndex(), bufferSize); if (len <= 0) break; inputData.writerIndex(inputData.writerIndex() + len); if (!formFrames(inputData)) break; inputData.discardReadBytes(); inputData.capacity(inputData.writerIndex() + bufferSize); } // while } catch (InterruptedIOException ex) { Logger.warn("nsd input timeout: {}", ex); try { inputStream.close(); } catch (IOException ioex) { } notifyDisconnect(); return; } catch (Exception ex) { Logger.warn("nsd input read failed: {}", ex); try { inputStream.close(); } catch (IOException ioex) { } notifyDisconnect(); return; } Logger.debug("nsd input read end"); notifyDisconnect(); }