List of usage examples for io.netty.buffer ByteBuf capacity
public abstract ByteBuf capacity(int newCapacity);
From source file:dorkbox.network.pipeline.ByteBufOutput.java
License:Apache License
/** Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted. * Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more * efficient than {@link #writeString(String)}. The string can be read using {@link ByteBufInput#readString()} or * {@link ByteBufInput#readStringBuilder()}. * @param value May be null. *//*from w ww. j a v a 2 s . c o m*/ @Override public void writeAscii(String value) throws KryoException { if (value == null) { writeByte(0x80); // 0 means null, bit 8 means UTF8. return; } int charCount = value.length(); if (charCount == 0) { writeByte(1 | 0x80); // 1 means empty string, bit 8 means UTF8. return; } ByteBuf buffer = byteBuf; if (buffer.writableBytes() < charCount) { buffer.capacity(buffer.capacity() + charCount + 1); } int charIndex = 0; // Try to write 8 bit chars. for (; charIndex < charCount; charIndex++) { int c = value.charAt(charIndex); buffer.writeByte((byte) c); } // specify it's ASCII int i = buffer.writerIndex() - 1; buffer.setByte(i, buffer.getByte(i) | 0x80); // Bit 8 means end of ASCII. }
From source file:impl.underdark.transport.bluetooth.BtLink.java
License:Open Source License
private void inputLoop() { // Input I/O thread. sendHelloFrame();/*from www . ja v a2 s . c o m*/ 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 {//from ww w .j a va 2 s .c om 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(); }
From source file:io.horizondb.client.MsgToByteEncoder.java
License:Apache License
/** * {@inheritDoc}/*w w w .j a va 2 s. c om*/ */ @Override protected void encode(ChannelHandlerContext ctx, Msg<?> msg, ByteBuf out) throws Exception { out.writerIndex(Buffers.wrap(out.capacity(msg.computeSerializedSize())).order(ByteOrder.LITTLE_ENDIAN) .writeObject(msg).writerIndex()); }
From source file:org.pumpkindb.Closure.java
License:Mozilla Public License
@Override public void encode(ByteBuf buffer) { ByteBuf buf = Unpooled.buffer(); encodables.forEach(encodable -> encodable.encode(buf)); int index = buf.writerIndex(); buf.capacity(index); new Data(buf.array()).encode(buffer); }