List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:jazmin.server.msg.codec.json.JSONDecoder.java
License:Open Source License
/** * Returns the index in the buffer of the end of line found. * Returns -1 if no end of line was found in the buffer. *//*from w w w .j a v a2s. c o m*/ private static int findEndOfLine(final ByteBuf buffer) { final int n = buffer.writerIndex(); for (int i = buffer.readerIndex(); i < n; i++) { final byte b = buffer.getByte(i); if (b == '\n') { return i; } else if (b == '\r' && i < n - 1 && buffer.getByte(i + 1) == '\n') { return i; // \r\n } } return -1; // Not found. }
From source file:me.nithanim.cultures.format.cif.CifFileWriter.java
@Override public void pack(CifFile o, ByteBuf buf) throws IOException { List<String> lines = o.getLines(); ByteBufAllocator alloc = ByteBufAllocator.DEFAULT; ByteBuf indexTable = alloc.buffer(o.getLines().size()).order(ByteOrder.LITTLE_ENDIAN); ByteBuf contentTable = alloc.buffer(o.getLines().size() * 10).order(ByteOrder.LITTLE_ENDIAN); if (o.getFileFormat() == CifFile.FileFormat.CIF) { for (String l : lines) { int idx = contentTable.writerIndex(); ByteBufUtil.hexDump(contentTable); indexTable.writeInt(idx);/* w w w .jav a 2s.com*/ l = l.trim(); if (l.startsWith("[")) { l = l.substring(1, l.length() - 1); contentTable.writeByte(1); } else { contentTable.writeByte(2); } contentTable.writeBytes(l.getBytes(CharsetUtil.ISO_8859_1)); contentTable.writeByte('\0'); } } else { for (String l : lines) { int idx = contentTable.writerIndex(); indexTable.writeInt(idx); l = l.trim(); contentTable.writeBytes(l.getBytes(CharsetUtil.ISO_8859_1)); contentTable.writeByte('\0'); } } EncryptedInformation ei = new EncryptedInformation(o.getLines().size(), indexTable.writerIndex(), indexTable, contentTable.writerIndex(), contentTable); Writer<EncryptedInformation> eiw; if (o.getInternalFormat() == CifFile.InternalFormat.TYPE1) { buf.writeInt(65601); eiw = type1Writer; } else if (o.getInternalFormat() == CifFile.InternalFormat.TYPE2) { buf.writeInt(1021); eiw = type2Writer; } else { throw new UnsupportedDataTypeException("The given data is not a cif file!"); } eiw.pack(ei, buf); }
From source file:mysql.client.Session_Old.java
final String readString(ByteBuf buf, String encoding) throws Exception { int i = buf.readerIndex(); int len = 0;/*from w ww . ja v a2s . c o m*/ int maxLen = buf.writerIndex(); while ((i < maxLen) && (buf.getByte(i) != 0)) { len++; i++; } try { return StringUtils.toString(buf.array(), buf.readerIndex(), len, encoding); } catch (UnsupportedEncodingException uEE) { throw uEE; } finally { buf.readerIndex(buf.readerIndex() + (len + 1)); } }
From source file:mysql.client.Session_Old.java
String readString(ByteBuf buf, String encoding, int expectedLength) throws Exception { if (buf.readerIndex() + expectedLength > buf.writerIndex()) { throw new RuntimeException(); }/*from ww w .j a v a2 s. c o m*/ try { return StringUtils.toString(buf.array(), buf.readerIndex(), expectedLength, encoding); } catch (UnsupportedEncodingException uEE) { throw uEE; } finally { buf.readerIndex(buf.readerIndex() + expectedLength); } }
From source file:mysql.client.Session_Old.java
public void send(ByteBuf packet) throws IOException { this.packetSequence++; int position = packet.writerIndex(); packet.readerIndex(0);/*w w w .j av a 2 s. c o m*/ packet.writerIndex(0); int size = position - 4; packet.writeByte((byte) (size & 0xff)); packet.writeByte((byte) (size >>> 8)); packet.writeByte((byte) (size >>> 16)); packet.writeByte(packetSequence); socket.getOutputStream().write(packet.array()); socket.getOutputStream().flush(); }
From source file:nenea.client.operation.OperationClientHandler.java
License:Apache License
private ByteBuf initializeProtocol(String msg, Channel ch) throws UnsupportedEncodingException { System.out.println("make JOIN Header"); ByteBuf buf = ch.alloc().heapBuffer(OP_CODE_JOIN.length() + 12 + msg.getBytes("UTF-8").length); buf.writeInt(OP_CODE_JOIN.length()); buf.writeBytes(OP_CODE_JOIN.getBytes()); buf.writeLong(msg.getBytes("UTF-8").length); buf.writeBytes(msg.getBytes("UTF-8")); System.out.println("buffer index : " + buf.writerIndex()); System.out.println("hex : " + ByteBufUtil.hexDump(buf)); return buf;/*ww w. ja v a2 s. c o m*/ }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterRequestEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ModbusRequest msg, ByteBuf out) throws Exception { int from = out.writerIndex(); out.writeShort(msg.getTransectionId()); out.writeShort(0);// w w w. jav a 2 s.c o m out.writeShort(2 + msg.getFunction().getRequestDataLength()); out.writeByte(msg.getUnitId()); out.writeByte(msg.getFunction().getCode()); msg.getFunction().encodeRequestData(out); if (withCheckSum) { out.writeShort(checkSum(out, from)); } }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterRequestEncoder.java
License:Open Source License
private int checkSum(ByteBuf out, int from) { return Utils.crc(out, from, out.writerIndex() - from); }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ModbusResponse msg, ByteBuf out) throws Exception { int from = out.writerIndex(); msg.encode(out);//from w w w . j a v a 2s.c o m if (withCheckSum) { out.writeShort(Utils.crc(out, from, out.writerIndex() - from)); } }
From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java
License:Apache License
@Override public AlternativeCompositeByteBuf getBytes(int index, ByteBuf dst, int length) { getBytes(index, dst, dst.writerIndex(), length); dst.writerIndex(dst.writerIndex() + length); return this; }