private void putBucketDataInFileHeader() { fileHeader.position(bucketPosition(bucketIndex)); fileHeader.putInt(bucketMessageId); fileHeader.putLong(bucketTimestamp); fileHeader.putInt(bucketCount); // data will be written at the next checkpoint }
@Override public ByteBuffer encode(ByteBuffer a, Have have) { ByteBuffer buf = prepareMessage(a, 4, MessageID.HAVE); buf.putInt(have.getPieceIndex()); buf.putInt(8, have.getAib()); return buf; }
/** * Get the data of this RTP packet as a byte array. * * @return The data of this RTP packet as a byte array. */ private ByteBuffer getData() { ByteBuffer byteBuffer = ByteBuffer.allocate(12 + payloadLength); /* Since V..SN are 32 bits, create a (int) byte array for V..SN. */ long V_SN = 0; V_SN = ((long) version) << 30 | this.padding << 29 | extension << 28 | csrcCount << 24 | this.marker << 23 | this.payloadType << 16 | (sequenceNumber & 0xffff); byteBuffer.putInt((int) V_SN); // offset = 4 from the start of packet byteBuffer.putInt((int) this.timeStamp); // offset = 8 from start of packet byteBuffer.putInt((int) this.SSRC); if (payloadLength != 0) { // This only applies if somebody has tinkered with the payload. // offset = 12 from start of packet. byteBuffer.put(payload); } // Reset pointer to start of buffer. byteBuffer.rewind(); return byteBuffer; }
public void newCheckpoint(byte[] state, byte[] stateHash, int consensusId) { String ckpPath = DEFAULT_DIR + String.valueOf(id) + "." + System.currentTimeMillis() + ".tmp"; try { checkpointLock.lock(); RandomAccessFile ckp = new RandomAccessFile(ckpPath, (syncCkp ? "rwd" : "rw")); ByteBuffer bf = ByteBuffer.allocate(state.length + stateHash.length + 4 * INT_BYTE_SIZE); bf.putInt(state.length); bf.put(state); bf.putInt(stateHash.length); bf.put(stateHash); bf.putInt(EOF); bf.putInt(consensusId); byte[] ckpState = bf.array(); ckp.write(ckpState); ckp.close(); if (isToLog) deleteLogFile(); deleteLastCkp(); renameCkp(ckpPath); if (isToLog) createLogFile(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { checkpointLock.unlock(); } }
@Test public void test64kColumn() { // a byte buffer more than 64k ByteBuffer buffer = ByteBuffer.allocate(1024 * 65); buffer.clear(); // read more than 64k for (int i = 0; i < 1024 * 64 / 4 + 1; i++) buffer.putInt(0); // for read buffer.flip(); Column column = new Column(ByteBufferUtil.bytes("test"), buffer, 0); SecondaryIndexColumnSizeTest.MockRowIndex mockRowIndex = new SecondaryIndexColumnSizeTest.MockRowIndex(); SecondaryIndexColumnSizeTest.MockColumnIndex mockColumnIndex = new SecondaryIndexColumnSizeTest.MockColumnIndex(); assertTrue(mockRowIndex.validate(column)); assertFalse(mockColumnIndex.validate(column)); // test less than 64k value buffer.flip(); buffer.clear(); buffer.putInt(20); buffer.flip(); assertTrue(mockRowIndex.validate(column)); assertTrue(mockColumnIndex.validate(column)); }
/** * Copy this ObjectId to an output writer in raw binary. * * @param w the buffer to copy to. Must be in big endian order. */ public void copyRawTo(final ByteBuffer w) { w.putInt(w1); w.putInt(w2); w.putInt(w3); w.putInt(w4); w.putInt(w5); }
public static String generateHallmark(String secretPhrase, String host, int weight, int date) { if (host.length() == 0 || host.length() > 100) { throw new IllegalArgumentException("Hostname length should be between 1 and 100"); } if (weight <= 0 || weight > Constants.MAX_BALANCE_NXT) { throw new IllegalArgumentException( "Weight should be between 1 and " + Constants.MAX_BALANCE_NXT); } byte[] publicKey = Crypto.getPublicKey(secretPhrase); byte[] hostBytes = Convert.toBytes(host); ByteBuffer buffer = ByteBuffer.allocate(32 + 2 + hostBytes.length + 4 + 4 + 1); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(publicKey); buffer.putShort((short) hostBytes.length); buffer.put(hostBytes); buffer.putInt(weight); buffer.putInt(date); byte[] data = buffer.array(); data[data.length - 1] = (byte) ThreadLocalRandom.current().nextInt(); byte[] signature = Crypto.sign(data, secretPhrase); return Convert.toHexString(data) + Convert.toHexString(signature); }
@Override public void getBox(WritableByteChannel writableByteChannel) throws IOException { writableByteChannel.write(getHeader()); ByteBuffer byteBuffer = ByteBuffer.allocate(52 + (fontName != null ? fontName.length() : 0)); byteBuffer.position(6); IsoTypeWriter.writeUInt16(byteBuffer, dataReferenceIndex); byteBuffer.putInt(displayFlags); byteBuffer.putInt(textJustification); IsoTypeWriter.writeUInt16(byteBuffer, backgroundR); IsoTypeWriter.writeUInt16(byteBuffer, backgroundG); IsoTypeWriter.writeUInt16(byteBuffer, backgroundB); IsoTypeWriter.writeUInt64(byteBuffer, defaultTextBox); IsoTypeWriter.writeUInt64(byteBuffer, reserved1); byteBuffer.putShort(fontNumber); byteBuffer.putShort(fontFace); byteBuffer.put(reserved2); byteBuffer.putShort(reserved3); IsoTypeWriter.writeUInt16(byteBuffer, foregroundR); IsoTypeWriter.writeUInt16(byteBuffer, foregroundG); IsoTypeWriter.writeUInt16(byteBuffer, foregroundB); if (fontName != null) { IsoTypeWriter.writeUInt8(byteBuffer, fontName.length()); byteBuffer.put(fontName.getBytes()); } writableByteChannel.write((ByteBuffer) byteBuffer.rewind()); // writeContainer(writableByteChannel); there are no child boxes!? }
@Override public void marshal(ReprocessFormatsMap value, ByteBuffer buffer) { /* * // writing (static example, DNG+ZSL) * int32_t[] contents = { * RAW_OPAQUE, 3, RAW16, YUV_420_888, BLOB, * RAW16, 2, YUV_420_888, BLOB, * ..., * INPUT_FORMAT, OUTPUT_FORMAT_COUNT, [OUTPUT_0, OUTPUT_1, ..., OUTPUT_FORMAT_COUNT-1] * }; */ int[] inputs = StreamConfigurationMap.imageFormatToInternal(value.getInputs()); for (int input : inputs) { // INPUT_FORMAT buffer.putInt(input); int[] outputs = StreamConfigurationMap.imageFormatToInternal(value.getOutputs(input)); // OUTPUT_FORMAT_COUNT buffer.putInt(outputs.length); // [OUTPUT_0, OUTPUT_1, ..., OUTPUT_FORMAT_COUNT-1] for (int output : outputs) { buffer.putInt(output); } } }
// Called when a peer we're trying to connect to sends us a message private void processNewPeer(SocketChannel c) throws IOException { ByteBuffer message = ChannelHelper.readBytes(c, 4); String recognize = utf8.decode(message).toString(); if (!recognize.equals("bam!")) { // Connected to something that wasn't a BAMPong client... c.close(); Peer p = new_peers.remove(c); log("Closing attempt to " + p.getName() + " got " + recognize); return; } // Assemble response ByteBuffer name = utf8.encode(nick); message = ByteBuffer.allocateDirect(name.limit() + 10); // id(4), port(4), name(2+limit) message.putInt(id); message.putInt(getPort()); ChannelHelper.putString(message, name); message.flip(); // Send message c.write(message); // Move socket to connected peers. Peer peer = new_peers.remove(c); peers.put(c, peer); sockets.put(peer, c); }
@Override public byte[] toBinary() { final ByteBuffer bb = ByteBuffer.allocate(8); bb.putInt(start); bb.putInt(end); return bb.array(); }
public static void writeUnsignedVarInt(int value, ByteBuffer dest) throws IOException { while ((value & 0xFFFFFF80) != 0L) { dest.putInt((value & 0x7F) | 0x80); value >>>= 7; } dest.putInt(value & 0x7F); }
/** * Render this header to a byte buffer. * * @param buf The target buffer. */ public void render(ByteBuffer buf) { buf.put(BinaryFormat.HEADER_MAGIC); buf.putShort(format.getFlagWord()); buf.putInt(ratingCount); buf.putInt(userCount); buf.putInt(itemCount); }
public static final void writeAsBytes(PrimerPack primerPack, ByteBuffer buffer) throws NullPointerException, InsufficientSpaceException { if (primerPack == null) throw new NullPointerException("Cannot write a null primer pack to a buffer."); if (buffer == null) throw new NullPointerException("Cannot write a primer pack into a null buffer."); if (buffer.remaining() < lengthAsBytes(primerPack)) throw new InsufficientSpaceException( "Insufficient space remaining to write primer pack into given buffer."); UL key = (UL) Forge.makeAUID( 0x0d010201, (short) 0x0105, (short) 0x0100, new byte[] {0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01}); MXFBuilder.writeKey(key, buffer); MXFBuilder.writeBERLength(18 * primerPack.countLocalTagEntries() + 8, 4, buffer); buffer.putInt(primerPack.countLocalTagEntries()); buffer.putInt(18); for (LocalTagEntry entry : primerPack.getLocalTagEntryBatch()) { buffer.putShort(entry.getLocalTag()); MXFBuilder.writeKey((UL) entry.getUID(), buffer); } }
// Dumps the contents of the file as ByteBuffer. public void readFile_ByteBuffer() throws Exception { OrcStruct lv_row = null; Object lv_field_val = null; ByteBuffer lv_row_buffer; while (m_rr.hasNext()) { byte[] lv_row_ba = new byte[4096]; lv_row_buffer = ByteBuffer.wrap(lv_row_ba); lv_row = (OrcStruct) m_rr.next(lv_row); for (int i = 0; i < m_fields.size(); i++) { lv_field_val = lv_row.getFieldValue(i); if (lv_field_val == null) { lv_row_buffer.putInt(0); continue; } String lv_field_val_str = lv_field_val.toString(); lv_row_buffer.putInt(lv_field_val_str.length()); if (lv_field_val != null) { lv_row_buffer.put(lv_field_val_str.getBytes()); } } System.out.println(lv_row_buffer); // System.out.println(new String(lv_row_buffer.array())); } }
/** get the data in this map as a ByteBuffer */ public ByteBuffer getData() { int size = getLength(); ByteBuffer buf = ByteBuffer.allocate(size); // write the header buf.putInt(getFormat()); buf.putInt(getItalicAngle()); buf.putShort(getUnderlinePosition()); buf.putShort(getUnderlineThickness()); buf.putShort(getIsFixedPitch()); buf.putShort((short) 0); buf.putInt(getMinMemType42()); buf.putInt(getMaxMemType42()); buf.putInt(getMinMemType1()); buf.putInt(getMaxMemType1()); // now write the table buf.put(nameMap.getData()); // reset the start pointer buf.flip(); return buf; }
public byte[] getNext() throws Exception { if (!m_rr.hasNext()) { return null; } OrcStruct lv_row = (OrcStruct) m_rr.next(null); Object lv_field_val = null; ByteBuffer lv_row_buffer; byte[] lv_row_ba = new byte[4096]; lv_row_buffer = ByteBuffer.wrap(lv_row_ba); for (int i = 0; i < m_fields.size(); i++) { lv_field_val = lv_row.getFieldValue(i); if (lv_field_val == null) { lv_row_buffer.putInt(0); continue; } String lv_field_val_str = lv_field_val.toString(); lv_row_buffer.putInt(lv_field_val_str.length()); if (lv_field_val != null) { lv_row_buffer.put(lv_field_val_str.getBytes()); } } System.out.println(lv_row_buffer); return lv_row_buffer.array(); }
private static ByteBuffer build(TcpPacket s) { /* except option and padding: 16 shorts */ ByteBuffer bb = ByteBuffer.allocate(12 + s.getTotalLength()); // TODO: IPv6 handling // pseudo header bb.putInt(IpConverter.toInt((Inet4Address) s.getSourceAddress())); bb.putInt(IpConverter.toInt((Inet4Address) s.getDestinationAddress())); bb.put((byte) 0); // padding bb.put((byte) 6); // tcp bb.putShort((short) (s.getTotalLength())); // bb.putShort((short) s.getSourcePort()); bb.putShort((short) s.getDestinationPort()); bb.putInt(s.getSeq()); bb.putInt(s.getAck()); bb.put((byte) (s.getDataOffset() << 4)); bb.put((byte) s.getFlags()); bb.putShort((short) s.getWindow()); bb.putShort((short) 0); // checksum bb.putShort((short) s.getUrgentPointer()); if (s.getOptions() != null) bb.put(s.getOptions()); if (s.getPadding() != null) bb.put(s.getPadding()); bb.flip(); return bb; }
protected void doWrite(ByteBuffer out) { super.doWrite(out); if ((flags & 0x1) != 0) // self ref return; out.put(asciiString(type), 0, 4); out.putShort(recordSize); out.putShort(version); out.putShort(kind); NIOUtils.writePascalStringL(out, volumeName, 27); out.putInt(volumeCreateDate); out.putShort(volumeSignature); out.putShort(volumeType); out.putInt(parentDirId); NIOUtils.writePascalStringL(out, fileName, 63); out.putInt(fileNumber); out.putInt(createdLocalDate); out.put(asciiString(fileTypeName), 0, 4); out.put(asciiString(creatorName), 0, 4); out.putShort(nlvlFrom); out.putShort(nlvlTo); out.putInt(volumeAttributes); out.putShort(fsId); out.put(new byte[10]); for (ExtraField extraField : extra) { out.putShort(extraField.type); out.putShort((short) extraField.len); out.put(extraField.data); } out.putShort((short) -1); out.putShort((short) 0); }
public void save(int pageNo, B_Tree.Page<Key> page) { try { ByteBuffer buffer = ByteBuffer.allocate(pageSize); List<B_Tree.KeyPointer<Key>> ptrs = page.keyPointers; boolean isBranch = !ptrs.isEmpty() && ptrs.get(0).t2 instanceof B_Tree.Branch; buffer.putChar(isBranch ? INTERNAL : LEAF); buffer.putInt(ptrs.size()); for (B_Tree.KeyPointer<Key> keyPtr : ptrs) { keyAccessor.write(buffer, keyPtr.t1); if (keyPtr.t2 instanceof B_Tree.Branch) { int branch = ((B_Tree.Branch) keyPtr.t2).branch; buffer.putInt(branch); } else if (keyPtr.t2 instanceof B_Tree.Leaf) { @SuppressWarnings("unchecked") Value value = ((B_Tree.Leaf<Value>) keyPtr.t2).value; valueAccessor.write(buffer, value); } } buffer.flip(); channel.write(buffer, pageNo * pageSize); } catch (IOException ex) { throw new RuntimeException(ex); } }
public static final ByteBuffer makeMessageBuffer( final List<Long> msgIds, final List<PutCommand> reqs) { if (msgIds == null || reqs == null) { throw new IllegalArgumentException("Null id list or request list"); } if (msgIds.size() != reqs.size()) { throw new IllegalArgumentException("id list is not adapte to request list"); } int capacity = 0; for (final PutCommand req : reqs) { capacity += 4 + 4 + 8 + 4 + req.getData().length; } final ByteBuffer buffer = ByteBuffer.allocate(capacity); for (int i = 0; i < reqs.size(); i++) { final PutCommand req = reqs.get(i); final long msgId = msgIds.get(i); buffer.putInt(req.getData().length); buffer.putInt(CheckSum.crc32(req.getData())); buffer.putLong(msgId); buffer.putInt(req.getFlag()); buffer.put(req.getData()); } buffer.flip(); return buffer; }
@Override public byte[] toBinary() { int byteBufferLength = 20 + (2 * orderedSfcIndexToTierId.size()); final List<byte[]> orderedSfcBinaries = new ArrayList<byte[]>(orderedSfcs.length); final List<byte[]> dimensionBinaries = new ArrayList<byte[]>(baseDefinitions.length); for (final SpaceFillingCurve sfc : orderedSfcs) { final byte[] sfcBinary = PersistenceUtils.toBinary(sfc); byteBufferLength += (4 + sfcBinary.length); orderedSfcBinaries.add(sfcBinary); } for (final NumericDimensionDefinition dimension : baseDefinitions) { final byte[] dimensionBinary = PersistenceUtils.toBinary(dimension); byteBufferLength += (4 + dimensionBinary.length); dimensionBinaries.add(dimensionBinary); } final ByteBuffer buf = ByteBuffer.allocate(byteBufferLength); buf.putInt(orderedSfcs.length); buf.putInt(baseDefinitions.length); buf.putInt(orderedSfcIndexToTierId.size()); buf.putLong(maxEstimatedDuplicateIds); for (final byte[] sfcBinary : orderedSfcBinaries) { buf.putInt(sfcBinary.length); buf.put(sfcBinary); } for (final byte[] dimensionBinary : dimensionBinaries) { buf.putInt(dimensionBinary.length); buf.put(dimensionBinary); } for (final Entry<Integer, Byte> entry : orderedSfcIndexToTierId.entrySet()) { buf.put(entry.getKey().byteValue()); buf.put(entry.getValue()); } return buf.array(); }
/** * 入力データからパケットを構築して送信する。今回はSocketを複数開いたり、 ということは起こらないので本メソッドに集約してしまってる。空文字列が送られてきたら * 特殊パターンとしてrefresh用のパケットを構築する(つまり\0単独は特殊パターン) * * @return 正常終了時は0。サーバへの接続が失われていれば-4。その他I/Oエラー時は-1。 */ private int sendPacket(String src) { OutputStream writer; Log.d("moku99", "sending a packet"); try { if (clientSock.isConnected() == false) { return -4; } writer = clientSock.getOutputStream(); if (src.equals("")) { // 空の特殊パターン ByteBuffer buf = ByteBuffer.allocate(8); buf.putInt(myId.intValue()); buf.putInt(0); writer.write(buf.array()); } else { // 通常メッセージ送信パターン byte[] strBuf = src.getBytes(); ByteBuffer buf = ByteBuffer.allocate(8 + strBuf.length); buf.putInt(myId.intValue()); buf.putInt(strBuf.length); buf.put(strBuf); writer.write(buf.array()); } } catch (IOException e) { Log.d("moku99", e.getLocalizedMessage()); return -1; } return 0; }
/* (non-Javadoc) * @see org.exist.storage.log.Loggable#write(java.nio.ByteBuffer) */ public void write(ByteBuffer out) { out.putInt((int) prevPage); out.putInt((int) pageNum); out.putInt((int) nextPage); out.putInt((int) oldPrev); out.putInt((int) oldNext); }
public byte[] makePacket() { int size = 8; if (serviceid == 100) { size += 100; } else if (serviceid == 200) { size += 12; } ByteBuffer headerBuf = ByteBuffer.allocate(size); headerBuf.putInt(serviceid); headerBuf.putInt(reqVersion); if (serviceid == 100) { byte[] tmp = new byte[100]; byte[] keywordBytes = keyword.getBytes(); System.arraycopy( keywordBytes, 0, tmp, 0, keywordBytes.length > 100 ? 100 : keywordBytes.length); headerBuf.put(tmp); } else if (serviceid == 200) { headerBuf.putInt(x); headerBuf.putInt(y); headerBuf.putInt(radius); } return headerBuf.array(); }
@Override public byte[] getBytes() { ByteBuffer byteBuffer = ByteBuffer.allocate(98); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(0, this.getConnectionId()); byteBuffer.putInt(8, this.getAction().value()); byteBuffer.putInt(12, this.getTransactionId()); byteBuffer.position(16); byteBuffer.put(infoHash.getBytes()); byteBuffer.position(36); byteBuffer.put(peerId.getBytes()); byteBuffer.putLong(56, downloaded); byteBuffer.putLong(64, left); byteBuffer.putLong(72, uploaded); byteBuffer.putInt(80, this.getEvent().value()); byteBuffer.putInt(84, peerInfo.getIpAddress()); byteBuffer.putInt(88, key); byteBuffer.putInt(92, numWant); byteBuffer.putChar(96, (char) peerInfo.getPort()); byteBuffer.flip(); return byteBuffer.array(); }
@Override public ByteBuffer write(ByteBuffer buff, Object obj) { if (!(obj instanceof Integer)) { return super.write(buff, obj); } int x = (Integer) obj; if (x < 0) { // -Integer.MIN_VALUE is smaller than 0 if (-x < 0 || -x > DataUtils.COMPRESSED_VAR_INT_MAX) { buff.put((byte) TAG_INTEGER_FIXED); buff.putInt(x); } else { buff.put((byte) TAG_INTEGER_NEGATIVE); DataUtils.writeVarInt(buff, -x); } } else if (x <= 15) { buff.put((byte) (TAG_INTEGER_0_15 + x)); } else if (x <= DataUtils.COMPRESSED_VAR_INT_MAX) { buff.put((byte) TYPE_INT); DataUtils.writeVarInt(buff, x); } else { buff.put((byte) TAG_INTEGER_FIXED); buff.putInt(x); } return buff; }
public void putToBuffer(ByteBuffer buffer) { buffer.put(requestType.getCode()); buffer.put(errorCode.getCode()); buffer.putInt(sku); buffer.putShort(store); buffer.putInt(amount); }
@Override void putMyBytes(ByteBuffer buffer) { buffer.putLong(goodsId); buffer.putInt(quantity); buffer.putLong(priceNQT); buffer.putInt(deliveryDeadlineTimestamp); }
/** in order to improve the likelihood of not corrupting the header write as a single operation */ protected void write(DataOutput out) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(16); buffer.putLong(dataPointer); buffer.putInt(dataCapacity); buffer.putInt(dataCount); out.write(buffer.array(), 0, 16); }