List of usage examples for java.nio ByteBuffer putLong
public abstract ByteBuffer putLong(long value);
From source file:org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer.java
public ByteBuffer serialize(MapRequest mapRequest) { int size = Length.HEADER_SIZE; if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getEid() != null) { size += LispAddressSerializer.getInstance().getAddressSize(mapRequest.getSourceEid().getEid()); } else {/*from www. ja v a2 s . co m*/ size += 2; } if (mapRequest.getItrRloc() != null) { for (ItrRloc address : mapRequest.getItrRloc()) { size += LispAddressSerializer.getInstance().getAddressSize(address.getRloc()); } } if (mapRequest.getEidItem() != null) { for (EidItem record : mapRequest.getEidItem()) { size += 2 + LispAddressSerializer.getInstance().getAddressSize(record.getEid()); } } ByteBuffer requestBuffer = ByteBuffer.allocate(size); requestBuffer.put((byte) ((byte) (MessageType.MapRequest.getIntValue() << 4) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isAuthoritative()), Flags.AUTHORITATIVE) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isMapDataPresent()), Flags.MAP_DATA_PRESENT) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isProbe()), Flags.PROBE) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmr()), Flags.SMR))); requestBuffer.put((byte) (ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isPitr()), Flags.PITR) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmrInvoked()), Flags.SMR_INVOKED))); if (mapRequest.getItrRloc() != null) { int IRC = mapRequest.getItrRloc().size(); if (IRC > 0) { IRC--; } requestBuffer.put((byte) (IRC)); } else { requestBuffer.put((byte) 0); } if (mapRequest.getEidItem() != null) { requestBuffer.put((byte) mapRequest.getEidItem().size()); } else { requestBuffer.put((byte) 0); } requestBuffer.putLong(NumberUtil.asLong(mapRequest.getNonce())); if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getEid() != null) { LispAddressSerializer.getInstance().serialize(requestBuffer, mapRequest.getSourceEid().getEid()); } else { requestBuffer.putShort((short) 0); } if (mapRequest.getItrRloc() != null) { for (ItrRloc address : mapRequest.getItrRloc()) { LispAddressSerializer.getInstance().serialize(requestBuffer, address.getRloc()); } } if (mapRequest.getEidItem() != null) { for (EidItem record : mapRequest.getEidItem()) { requestBuffer.put((byte) 0); requestBuffer.put((byte) MaskUtil.getMaskForAddress(record.getEid().getAddress())); LispAddressSerializer.getInstance().serialize(requestBuffer, record.getEid()); } } if (mapRequest.getMapReply() != null) { ByteBuffer replyBuffer = ByteBuffer.allocate(MappingRecordSerializer.getInstance() .getSerializationSize(mapRequest.getMapReply().getMappingRecord())); MappingRecordSerializer.getInstance().serialize(replyBuffer, mapRequest.getMapReply().getMappingRecord()); ByteBuffer combinedBuffer = ByteBuffer.allocate(requestBuffer.capacity() + replyBuffer.capacity()); combinedBuffer.put(requestBuffer.array()); combinedBuffer.put(replyBuffer.array()); return combinedBuffer; } return requestBuffer; }
From source file:org.apache.hadoop.hdfs.server.datanode.BlockSender.java
/** * Sends upto maxChunks chunks of data./*from w w w . j av a 2s .c o m*/ * * When blockInPosition is >= 0, assumes 'out' is a * {@link SocketOutputStream} and tries * {@link SocketOutputStream#transferToFully(FileChannel, long, int)} to * send data (and updates blockInPosition). */ private int sendChunks(ByteBuffer pkt, int maxChunks, OutputStream out) throws IOException { // Sends multiple chunks in one packet with a single write(). int len = Math.min((int) (endOffset - offset), bytesPerChecksum * maxChunks); // truncate len so that any partial chunks will be sent as a final packet. // this is not necessary for correctness, but partial chunks are // ones that may be recomputed and sent via buffer copy, so try to minimize // those bytes if (len > bytesPerChecksum && len % bytesPerChecksum != 0) { len -= len % bytesPerChecksum; } if (len == 0) { return 0; } int numChunks = (len + bytesPerChecksum - 1) / bytesPerChecksum; int packetLen = len + numChunks * checksumSize + 4; pkt.clear(); // write packet header pkt.putInt(packetLen); pkt.putLong(offset); pkt.putLong(seqno); pkt.put((byte) ((offset + len >= endOffset) ? 1 : 0)); //why no ByteBuf.putBoolean()? pkt.putInt(len); int checksumOff = pkt.position(); int checksumLen = numChunks * checksumSize; byte[] buf = pkt.array(); if (checksumSize > 0 && checksumIn != null) { try { checksumIn.readFully(buf, checksumOff, checksumLen); } catch (IOException e) { LOG.warn(" Could not read or failed to veirfy checksum for data" + " at offset " + offset + " for block " + block + " got : " + StringUtils.stringifyException(e)); IOUtils.closeStream(checksumIn); checksumIn = null; if (corruptChecksumOk) { if (checksumOff < checksumLen) { // Just fill the array with zeros. Arrays.fill(buf, checksumOff, checksumLen, (byte) 0); } } else { throw e; } } } int dataOff = checksumOff + checksumLen; if (blockInPosition < 0) { //normal transfer IOUtils.readFully(blockIn, buf, dataOff, len); if (verifyChecksum) { int dOff = dataOff; int cOff = checksumOff; int dLeft = len; for (int i = 0; i < numChunks; i++) { checksum.reset(); int dLen = Math.min(dLeft, bytesPerChecksum); checksum.update(buf, dOff, dLen); if (!checksum.compare(buf, cOff)) { throw new ChecksumException("Checksum failed at " + (offset + len - dLeft), len); } dLeft -= dLen; dOff += dLen; cOff += checksumSize; } } // only recompute checksum if we can't trust the meta data due to // concurrent writes if (memoizedBlock.hasBlockChanged(len)) { ChecksumUtil.updateChunkChecksum(buf, checksumOff, dataOff, len, checksum); } try { out.write(buf, 0, dataOff + len); } catch (IOException e) { throw ioeToSocketException(e); } } else { try { //use transferTo(). Checks on out and blockIn are already done. SocketOutputStream sockOut = (SocketOutputStream) out; FileChannel fileChannel = ((FileInputStream) blockIn).getChannel(); if (memoizedBlock.hasBlockChanged(len)) { fileChannel.position(blockInPosition); IOUtils.readFileChannelFully(fileChannel, buf, dataOff, len); ChecksumUtil.updateChunkChecksum(buf, checksumOff, dataOff, len, checksum); sockOut.write(buf, 0, dataOff + len); } else { //first write the packet sockOut.write(buf, 0, dataOff); // no need to flush. since we know out is not a buffered stream. sockOut.transferToFully(fileChannel, blockInPosition, len); } blockInPosition += len; } catch (IOException e) { /* exception while writing to the client (well, with transferTo(), * it could also be while reading from the local file). */ throw ioeToSocketException(e); } } if (throttler != null) { // rebalancing so throttle throttler.throttle(packetLen); } return len; }
From source file:org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer.java
public ByteBuffer serialize(MapRequest mapRequest) { int size = Length.HEADER_SIZE; if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getLispAddressContainer() != null) { size += LispAddressSerializer.getInstance() .getAddressSize(LispAFIConvertor.toAFI(mapRequest.getSourceEid().getLispAddressContainer())); } else {//from ww w. j a v a 2s .c o m size += 2; } if (mapRequest.getItrRloc() != null) { for (ItrRloc address : mapRequest.getItrRloc()) { size += LispAddressSerializer.getInstance() .getAddressSize(LispAFIConvertor.toAFI(address.getLispAddressContainer())); } } if (mapRequest.getEidRecord() != null) { for (EidRecord record : mapRequest.getEidRecord()) { size += 2 + LispAddressSerializer.getInstance() .getAddressSize(LispAFIConvertor.toAFI(record.getLispAddressContainer())); } } ByteBuffer requestBuffer = ByteBuffer.allocate(size); requestBuffer.put((byte) ((byte) (LispMessageEnum.MapRequest.getValue() << 4) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isAuthoritative()), Flags.AUTHORITATIVE) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isMapDataPresent()), Flags.MAP_DATA_PRESENT) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isProbe()), Flags.PROBE) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmr()), Flags.SMR))); requestBuffer.put((byte) (ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isPitr()), Flags.PITR) | ByteUtil.boolToBit(BooleanUtils.isTrue(mapRequest.isSmrInvoked()), Flags.SMR_INVOKED))); if (mapRequest.getItrRloc() != null) { int IRC = mapRequest.getItrRloc().size(); if (IRC > 0) { IRC--; } requestBuffer.put((byte) (IRC)); } else { requestBuffer.put((byte) 0); } if (mapRequest.getEidRecord() != null) { requestBuffer.put((byte) mapRequest.getEidRecord().size()); } else { requestBuffer.put((byte) 0); } requestBuffer.putLong(NumberUtil.asLong(mapRequest.getNonce())); if (mapRequest.getSourceEid() != null && mapRequest.getSourceEid().getLispAddressContainer() != null) { LispAddressSerializer.getInstance().serialize(requestBuffer, LispAFIConvertor.toAFI(mapRequest.getSourceEid().getLispAddressContainer())); } else { requestBuffer.putShort((short) 0); } if (mapRequest.getItrRloc() != null) { for (ItrRloc address : mapRequest.getItrRloc()) { LispAddressSerializer.getInstance().serialize(requestBuffer, LispAFIConvertor.toAFI(address.getLispAddressContainer())); } } if (mapRequest.getEidRecord() != null) { for (EidRecord record : mapRequest.getEidRecord()) { requestBuffer.put((byte) 0); requestBuffer.put((byte) record.getMask().byteValue()); LispAddressSerializer.getInstance().serialize(requestBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer())); } } if (mapRequest.getMapReply() != null) { ByteBuffer replyBuffer = ByteBuffer.allocate( EidToLocatorRecordSerializer.getInstance().getSerializationSize(mapRequest.getMapReply())); EidToLocatorRecordSerializer.getInstance().serialize(replyBuffer, mapRequest.getMapReply()); ByteBuffer combinedBuffer = ByteBuffer.allocate(requestBuffer.capacity() + replyBuffer.capacity()); combinedBuffer.put(requestBuffer.array()); combinedBuffer.put(replyBuffer.array()); return combinedBuffer; } return requestBuffer; }
From source file:org.apache.bookkeeper.bookie.Bookie.java
/** * Fences a ledger. From this point on, clients will be unable to * write to this ledger. Only recoveryAddEntry will be * able to add entries to the ledger.// w w w . java 2 s .c o m * This method is idempotent. Once a ledger is fenced, it can * never be unfenced. Fencing a fenced ledger has no effect. */ public Future<Boolean> fenceLedger(long ledgerId, byte[] masterKey) throws IOException, BookieException { LedgerDescriptor handle = handles.getHandle(ledgerId, masterKey); boolean success; synchronized (handle) { success = handle.setFenced(); } if (success) { // fenced first time, we should add the key to journal ensure we can rebuild ByteBuffer bb = ByteBuffer.allocate(8 + 8); bb.putLong(ledgerId); bb.putLong(METAENTRY_ID_FENCE_KEY); bb.flip(); FutureWriteCallback fwc = new FutureWriteCallback(); LOG.debug("record fenced state for ledger {} in journal.", ledgerId); journal.logAddEntry(bb, fwc, null); return fwc.getResult(); } else { // already fenced return SUCCESS_FUTURE; } }
From source file:org.apache.bookkeeper.bookie.Bookie.java
/** * Retrieve the ledger descriptor for the ledger which entry should be added to. * The LedgerDescriptor returned from this method should be eventually freed with * #putHandle()./* w w w . j a va2 s. c om*/ * * @throws BookieException if masterKey does not match the master key of the ledger */ private LedgerDescriptor getLedgerForEntry(ByteBuffer entry, byte[] masterKey) throws IOException, BookieException { long ledgerId = entry.getLong(); LedgerDescriptor l = handles.getHandle(ledgerId, masterKey); if (!masterKeyCache.containsKey(ledgerId)) { // new handle, we should add the key to journal ensure we can rebuild ByteBuffer bb = ByteBuffer.allocate(8 + 8 + 4 + masterKey.length); bb.putLong(ledgerId); bb.putLong(METAENTRY_ID_LEDGER_KEY); bb.putInt(masterKey.length); bb.put(masterKey); bb.flip(); if (null == masterKeyCache.putIfAbsent(ledgerId, masterKey)) { journal.logAddEntry(bb, new NopWriteCallback(), null); } } return l; }
From source file:com.healthmarketscience.jackcess.Column.java
/** * Writes "Currency" values./*from www . j av a 2s. com*/ */ private static void writeCurrencyValue(ByteBuffer buffer, Object value) throws IOException { Object inValue = value; try { BigDecimal decVal = toBigDecimal(value); inValue = decVal; // adjust scale (will cause the an ArithmeticException if number has too // many decimal places) decVal = decVal.setScale(4); // now, remove scale and convert to long (this will throw if the value is // too big) buffer.putLong(decVal.movePointRight(4).longValueExact()); } catch (ArithmeticException e) { throw (IOException) new IOException("Currency value '" + inValue + "' out of range").initCause(e); } }
From source file:org.apache.hadoop.hbase.io.hfile.HFileBlock.java
/** * Adds metadata at current position (position is moved forward). Does not flip or reset. * @return The passed <code>destination</code> with metadata added. *//* w w w . ja va 2s.c o m*/ private ByteBuffer addMetaData(final ByteBuffer destination) { destination.put(this.fileContext.isUseHBaseChecksum() ? (byte) 1 : (byte) 0); destination.putLong(this.offset); destination.putInt(this.nextBlockOnDiskSize); return destination; }
From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java
/** * The weird constant above is to try to avoid mistakes in the painful (but * totally worth it) byte'ification method below. Using bytes as opposed to * json strings makes a non-trivial difference (~2x over json-smart and >4x * over org.json. So we just chuck json libraries and use our own byte[] * serializer for select packets.//from w w w . j a v a 2 s. co m * * The serialization overhead really matters most for RequestPacket and * AcceptPacket. Every request, even with batching, must be deserialized by * the coordinator and must be serialized back while sending out the * AcceptPacket. The critical path is the following at a coordinator and is * incurred at least in part even with batching for every request: (1) * receive request, (2) send accept, (3) receive accept_replies, (4) send * commit Accordingly, we use byteification for {@link RequestPacket}, * {@link AcceptPacket}, {@link BatchedAcceptReply} and * {@link BatchedCommit}. * * */ protected byte[] toBytes(boolean instrument) { // return cached value if already present if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) && this.byteifiedSelf != null && !instrument) return this.byteifiedSelf; // check if we can use byteification at all; if not, use toString() if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) { try { if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache return this.toString().getBytes(CHARSET); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); return null; } } // else byteify try { int exactLength = 0; byte[] array = new byte[this.lengthEstimate()]; ByteBuffer bbuf = ByteBuffer.wrap(array); assert (bbuf.position() == 0); // paxospacket stuff super.toBytes(bbuf); int ppPos = bbuf.position(); // for assertion assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != " + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED; exactLength += (bbuf.position()); bbuf.putLong(this.requestID); bbuf.put(this.stop ? (byte) 1 : (byte) 0); exactLength += (Long.BYTES + 1); // addresses /* Note: 0 is ambiguous with wildcard address, but that's okay * because an incoming packet will never come with a wildcard * address. */ bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0); /* Note: 0 is an ambiguous wildcard address that could also be a * legitimate value of the listening socket address. If the request * happens to have no listening address, we will end up assuming it * was received on the wildcard address. At worst, the matching for * the corresponding response back to the client can fail. */ bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0); exactLength += 2 * (Integer.BYTES + Short.BYTES); // other non-final fields bbuf.putInt(this.entryReplica); bbuf.putLong(this.entryTime); bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0); bbuf.putInt(this.forwardCount); exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES); // digest related fields: broadcasted, digest // whether this request was already broadcasted bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0); exactLength += 1; assert (exactLength == // where parent left us off ppPos + SIZEOF_REQUEST_FIXED // for the three int fields not yet filled - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - " + 4 * Integer.BYTES + "]"; // digest length and digest iteself bbuf.putInt(this.digest != null ? this.digest.length : 0); exactLength += Integer.BYTES; if (this.digest != null) bbuf.put(this.digest); exactLength += (this.digest != null ? this.digest.length : 0); // /////////// end of digest related fields ////////// // highly variable length fields // requestValue byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0); bbuf.put(reqValBytes); exactLength += (4 + reqValBytes.length); // responseValue byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(respValBytes != null ? respValBytes.length : 0); bbuf.put(respValBytes); exactLength += (4 + respValBytes.length); // batched requests batchSize|(length:batchedReqBytes)+ bbuf.putInt(this.batchSize()); exactLength += (4); if (this.batchSize() > 0) for (RequestPacket req : this.batched) { byte[] element = req.toBytes(); bbuf.putInt(element.length); bbuf.put(element); exactLength += (4 + element.length); } // bbuf.array() was a generous allocation byte[] exactBytes = new byte[exactLength]; bbuf.flip(); assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength; bbuf.get(exactBytes); if (this.getType() == PaxosPacketType.REQUEST) this.byteifiedSelf = exactBytes; return exactBytes; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:com.github.ambry.commons.BlobIdTest.java
/** * Build a string that resembles a bad blobId. * @param version The version number to be embedded in the blobId. * @param type The {@link BlobIdType} of the blobId. * @param datacenterId The datacenter id to be embedded in the blobId. * @param accountId The account id to be embedded in the blobId. * @param containerId The container id to be embedded in the blobId. * @param partitionId The partition id to be embedded in the blobId. * @param uuidLength The length of the uuid. * @param uuid The UUID to be embedded in the blobId. * @param extraChars Extra characters to put at the end of the ID. * @return a base-64 encoded {@link String} representing the blobId. */// ww w. j a v a2 s . c om private String buildBadBlobId(short version, BlobIdType type, Byte datacenterId, Short accountId, Short containerId, PartitionId partitionId, int uuidLength, String uuid, String extraChars) { int idLength; ByteBuffer idBuf; switch (version) { case BLOB_ID_V1: idLength = 2 + partitionId.getBytes().length + 4 + uuid.length() + extraChars.length(); idBuf = ByteBuffer.allocate(idLength); idBuf.putShort(version); break; case BLOB_ID_V2: idLength = 2 + 1 + 1 + 2 + 2 + partitionId.getBytes().length + 4 + uuid.length() + extraChars.length(); idBuf = ByteBuffer.allocate(idLength); idBuf.putShort(version); idBuf.put((byte) 0); idBuf.put(datacenterId); idBuf.putShort(accountId); idBuf.putShort(containerId); break; case BLOB_ID_V3: case BLOB_ID_V4: case BLOB_ID_V5: case BLOB_ID_V6: idLength = 2 + 1 + 1 + 2 + 2 + partitionId.getBytes().length + 4 + uuid.length() + extraChars.length(); idBuf = ByteBuffer.allocate(idLength); idBuf.putShort(version); idBuf.put((byte) type.ordinal()); idBuf.put(datacenterId); idBuf.putShort(accountId); idBuf.putShort(containerId); break; default: idLength = 2 + partitionId.getBytes().length + 4 + uuid.length() + extraChars.length(); idBuf = ByteBuffer.allocate(idLength); idBuf.putShort(version); break; } idBuf.put(partitionId.getBytes()); switch (version) { case BLOB_ID_V6: UUID uuidObj = UUID.fromString(uuid); idBuf.putLong(uuidObj.getMostSignificantBits()); idBuf.putLong(uuidObj.getLeastSignificantBits()); break; default: idBuf.putInt(uuidLength); idBuf.put(uuid.getBytes()); } idBuf.put(extraChars.getBytes()); return Base64.encodeBase64URLSafeString(idBuf.array()); }
From source file:com.healthmarketscience.jackcess.Column.java
/** * Writes a date value.//from ww w. ja va 2 s . c o m */ private void writeDateValue(ByteBuffer buffer, Object value) { if (value == null) { buffer.putDouble(0d); } else if (value instanceof DateExt) { // this is a Date value previously read from readDateValue(). use the // original bits to store the value so we don't lose any precision buffer.putLong(((DateExt) value).getDateBits()); } else { buffer.putDouble(toDateDouble(value)); } }