List of usage examples for io.netty.buffer ByteBufAllocator DEFAULT
ByteBufAllocator DEFAULT
To view the source code for io.netty.buffer ByteBufAllocator DEFAULT.
Click Source Link
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
protected DataBlock decodeRecord(ByteBuf buf, int length) { FtpSeekAheadData sad = null;/*from w w w .j a va 2 s. co m*/ try { sad = new FtpSeekAheadData(buf); } catch (SeekAheadNoBackArrayException e1) { return decodeRecordStandard(buf, length); } ByteBuf newbuf = ByteBufAllocator.DEFAULT.buffer(length); if (lastbyte == 0xFF) { int nextbyte = sad.bytes[sad.pos++]; if (nextbyte == 0xFF) { newbuf.writeByte((byte) (lastbyte & 0xFF)); } else { if (nextbyte == 1) { dataBlock.setEOR(true); } else if (nextbyte == 2) { dataBlock.setEOF(true); } else if (nextbyte == 3) { dataBlock.setEOR(true); dataBlock.setEOF(true); } lastbyte = 0; } } try { while (sad.pos < sad.limit) { lastbyte = sad.bytes[sad.pos++]; if (lastbyte == 0xFF) { int nextbyte = sad.bytes[sad.pos++]; if (nextbyte == 0xFF) { newbuf.writeByte((byte) (lastbyte & 0xFF)); } else { if (nextbyte == 1) { dataBlock.setEOR(true); } else if (nextbyte == 2) { dataBlock.setEOF(true); } else if (nextbyte == 3) { dataBlock.setEOR(true); dataBlock.setEOF(true); } } } else { newbuf.writeByte((byte) (lastbyte & 0xFF)); } lastbyte = 0; } } catch (IndexOutOfBoundsException e) { // End of read } sad.setReadPosition(0); dataBlock.setBlock(newbuf); return dataBlock; }
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
protected ByteBuf encodeRecordStandard(DataBlock msg, ByteBuf buffer) { ByteBuf newbuf = ByteBufAllocator.DEFAULT.buffer(msg.getByteCount()); int newbyte = 0; try {//from w ww.ja v a2s . co m while (true) { newbyte = buffer.readByte(); if (newbyte == 0xFF) { newbuf.writeByte((byte) (newbyte & 0xFF)); } newbuf.writeByte((byte) (newbyte & 0xFF)); } } catch (IndexOutOfBoundsException e) { // end of read } int value = 0; if (msg.isEOF()) { value += 2; } if (msg.isEOR()) { value += 1; } if (value > 0) { newbuf.writeByte((byte) 0xFF); newbuf.writeByte((byte) (value & 0xFF)); } msg.clear(); return newbuf; }
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
protected ByteBuf encodeRecord(DataBlock msg, ByteBuf buffer) { FtpSeekAheadData sad = null;/*from w w w . jav a2 s .c o m*/ try { sad = new FtpSeekAheadData(buffer); } catch (SeekAheadNoBackArrayException e1) { return encodeRecordStandard(msg, buffer); } ByteBuf newbuf = ByteBufAllocator.DEFAULT.buffer(msg.getByteCount()); int newbyte = 0; try { while (sad.pos < sad.limit) { newbyte = sad.bytes[sad.pos++]; if (newbyte == 0xFF) { newbuf.writeByte((byte) (newbyte & 0xFF)); } newbuf.writeByte((byte) (newbyte & 0xFF)); } } catch (IndexOutOfBoundsException e) { // end of read } int value = 0; if (msg.isEOF()) { value += 2; } if (msg.isEOR()) { value += 1; } if (value > 0) { newbuf.writeByte((byte) 0xFF); newbuf.writeByte((byte) (value & 0xFF)); } msg.clear(); sad.setReadPosition(0); return newbuf; }
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
/** * Encode a DataBlock in the correct format for Mode * //from ww w .ja v a2 s. c o m * @param msg * @return the ByteBuf or null when the last block is already done * @throws InvalidArgumentException */ protected ByteBuf encode(DataBlock msg) throws InvalidArgumentException { if (msg.isCleared()) { return null; } ByteBuf buffer = msg.getBlock(); if (mode == TransferMode.STREAM) { // If record structure, special attention if (structure == TransferStructure.RECORD) { return encodeRecord(msg, buffer); } msg.clear(); return buffer; } else if (mode == TransferMode.BLOCK) { int length = msg.getByteCount(); ByteBuf newbuf = ByteBufAllocator.DEFAULT.buffer(length > 0xFFFF ? 0xFFFF + 3 : length + 3); byte[] header = new byte[3]; // Is there any data left if (length == 0) { // It could be an empty block for EOR or EOF if (msg.isEOF() || msg.isEOR()) { header[0] = msg.getDescriptor(); header[1] = 0; header[2] = 0; newbuf.writeBytes(header); // Next call will be the last one msg.clear(); // return the last block return newbuf; } // This was the very last call msg.clear(); // return the end of encode return null; } // Is this a Restart so only Markers if (msg.isRESTART()) { header[0] = msg.getDescriptor(); header[1] = msg.getByteCountUpper(); header[2] = msg.getByteCountLower(); newbuf.writeBytes(header); newbuf.writeBytes(msg.getByteMarkers()); // Next call will be the last one msg.clear(); // return the last block return newbuf; } // Work on sub block, ignoring descriptor since it is not the last // one if (length > 0xFFFF) { header[0] = 0; header[1] = (byte) 0xFF; header[2] = (byte) 0xFF; newbuf.writeBytes(header); // Now take the first 0xFFFF bytes from buffer newbuf.writeBytes(msg.getBlock(), 0xFFFF); length -= 0xFFFF; msg.setByteCount(length); // return the sub block return newbuf; } // Last final block, using the descriptor header[0] = msg.getDescriptor(); header[1] = msg.getByteCountUpper(); header[2] = msg.getByteCountLower(); newbuf.writeBytes(header); // real data newbuf.writeBytes(buffer, length); // Next call will be the last one msg.clear(); // return the last block return newbuf; } // Mode unimplemented throw new InvalidArgumentException("Mode unimplemented: " + mode.name()); }
From source file:qunar.tc.qmq.store.IndexLog.java
License:Apache License
private AppendMessageResult doAppendData(final LogSegment segment, final ByteBuffer data) { int currentPos = segment.getWrotePosition(); final int freeSize = segment.getFileSize() - currentPos; if (data.remaining() <= freeSize) { if (!segment.appendData(data)) throw new RuntimeException("append index data failed."); return new AppendMessageResult<>(SUCCESS, segment.getBaseOffset() + segment.getWrotePosition()); }/* ww w . j a v a 2 s . c o m*/ ByteBuf to = ByteBufAllocator.DEFAULT.ioBuffer(freeSize); try { partialCopy(data, to); if (to.isWritable(Long.BYTES)) { to.writeLong(-1); } fillZero(to); if (!segment.appendData(to.nioBuffer())) throw new RuntimeException("append index data failed."); } finally { ReferenceCountUtil.release(to); } return new AppendMessageResult(END_OF_FILE, segment.getBaseOffset() + segment.getFileSize()); }
From source file:qunar.tc.qmq.store.MessageMemTable.java
License:Apache License
public MessageMemTable(final long tabletId, final long beginOffset, final int capacity) { this.firstSequences = new HashMap<>(); this.indexes = new HashMap<>(); this.mem = ByteBufAllocator.DEFAULT.ioBuffer(capacity); this.rwLock = new ReentrantReadWriteLock(); this.tabletId = tabletId; this.beginOffset = beginOffset; }
From source file:qunar.tc.qmq.sync.master.MessageIndexSyncWorker.java
License:Apache License
@Override protected SegmentBuffer getSyncLog(SyncRequest syncRequest) { final long originalOffset = syncRequest.getMessageLogOffset(); long startSyncOffset = originalOffset; long minMessageOffset = storage.getMinMessageOffset(); if (startSyncOffset < minMessageOffset) { startSyncOffset = minMessageOffset; LOG.info("reset message log sync offset from {} to {}", originalOffset, startSyncOffset); }// ww w . j a v a 2 s. c om try (MessageLogRecordVisitor visitor = storage.newMessageLogVisitor(startSyncOffset)) { LogSegment logSegment = null; ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(batchSize); long nextSyncOffset = startSyncOffset; try { for (int i = 0; i < MAX_SYNC_NUM; ++i) { LogVisitorRecord<MessageLogRecord> record = visitor.nextRecord(); if (record.isNoMore()) { nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize(); break; } if (!record.hasData()) { nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize(); continue; } MessageLogRecord data = record.getData(); logSegment = data.getLogSegment(); if (!byteBuf.isWritable(Long.BYTES)) break; byteBuf.markWriterIndex(); byteBuf.writeLong(data.getSequence()); if (!byteBuf.isWritable(Long.BYTES)) { byteBuf.resetWriterIndex(); break; } ByteBuffer body = data.getPayload(); //skip flag body.get(); long createTime = body.getLong(); //skip expireTime body.getLong(); //subject short len = body.getShort(); byte[] subject = new byte[len]; body.get(subject); //message id len = body.getShort(); byte[] messageId = new byte[len]; body.get(messageId); byteBuf.writeLong(createTime); if (!byteBuf.isWritable(Short.BYTES + subject.length)) { byteBuf.resetWriterIndex(); break; } PayloadHolderUtils.writeString(subject, byteBuf); if (!byteBuf.isWritable(Short.BYTES + messageId.length)) { byteBuf.resetWriterIndex(); break; } PayloadHolderUtils.writeString(messageId, byteBuf); nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize(); } } finally { if (!byteBuf.isReadable()) { byteBuf.release(); } } if (originalOffset == nextSyncOffset) { return null; } //FIXME: ???? if (byteBuf.isReadable()) { return new ByteBufSegmentBuffer(nextSyncOffset, logSegment, byteBuf, byteBuf.readableBytes()); } else { return new ByteBufSegmentBuffer(nextSyncOffset); } } }
From source file:qunar.tc.qmq.utils.HeaderSerializer.java
License:Apache License
public static ByteBuf serialize(RemotingHeader header, int payloadSize, int additional) { short headerSize = MIN_HEADER_SIZE; int bufferLength = TOTAL_SIZE_LEN + HEADER_SIZE_LEN + headerSize + additional; ByteBuf buffer = ByteBufAllocator.DEFAULT.ioBuffer(bufferLength); // total len//from ww w . j a v a 2s . co m int total = HEADER_SIZE_LEN + headerSize + payloadSize; buffer.writeInt(total); // header size buffer.writeShort(headerSize); // magic code buffer.writeInt(header.getMagicCode()); // code buffer.writeShort(header.getCode()); // version buffer.writeShort(header.getVersion()); // opaque buffer.writeInt(header.getOpaque()); // flag buffer.writeInt(header.getFlag()); buffer.writeShort(header.getRequestCode()); return buffer; }
From source file:reactor.ipc.netty.ByteBufFlux.java
License:Open Source License
/** * Decorate as {@link ByteBufFlux}/*from w w w . j a v a2 s. c o m*/ * * @param source publisher to decorate * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromInbound(Publisher<?> source) { return fromInbound(source, ByteBufAllocator.DEFAULT); }
From source file:reactor.ipc.netty.ByteBufFlux.java
License:Open Source License
/** * Open a {@link java.nio.channels.FileChannel} from a path and stream * {@link ByteBuf }chunks with/*from ww w .j a v a 2 s.c o m*/ * a given maximum size into the returned {@link ByteBufFlux} * * @param path the path to the resource to stream * @param maxChunkSize the maximum per-item ByteBuf size * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromPath(Path path, int maxChunkSize) { return fromPath(path, maxChunkSize, ByteBufAllocator.DEFAULT); }