List of usage examples for java.nio ByteBuffer getLong
public abstract long getLong();
From source file:com.openteach.diamond.network.waverider.network.Packet.java
/** * ByteBuffer??//from w w w . j a v a 2s . c o m * @param buffer * @return */ public static Packet unmarshall(ByteBuffer buffer) { if (buffer.remaining() < getHeaderSize()) { throw new RuntimeException("Wrong packet."); } Packet packet = new Packet(); byte[] str = new byte[NetWorkConstants.WAVERIDER_MAGIC.getBytes().length]; buffer.get(str); packet.setMagic(new String(str)); if (!NetWorkConstants.WAVERIDER_MAGIC.equals(packet.getMagic())) { throw new RuntimeException("Wrong packet."); } packet.setSequence(buffer.getLong()); packet.setType(buffer.getLong()); packet.setLength(buffer.getInt()); packet.setPayLoad(buffer.slice()); return packet; }
From source file:net.servicestack.client.Utils.java
public static UUID fromGuidBytes(byte[] guidBytes) { ByteBuffer buf = ByteBuffer.wrap(guidBytes); byte[] first4 = new byte[4]; buf.get(first4);/*w ww. jav a2s. c o m*/ reverse(first4); byte[] second2 = new byte[2]; buf.get(second2); reverse(second2); byte[] third2 = new byte[2]; buf.get(third2); reverse(third2); long lsb = buf.getLong(); buf = ByteBuffer.wrap(new byte[8]).put(first4).put(second2).put(third2); buf.rewind(); long msb = buf.getLong(); return new UUID(msb, lsb); }
From source file:com.twitter.distributedlog.DLSN.java
/** * Deserialize the DLSN from bytes array. * * @param data/* w ww .j a v a 2 s . c o m*/ * serialized bytes * @return dlsn */ public static DLSN deserializeBytes(byte[] data) { ByteBuffer bb = ByteBuffer.wrap(data); byte version = bb.get(); if (VERSION0 == version) { if (VERSION0_LEN != data.length) { throw new IllegalArgumentException("Invalid version zero DLSN " + Hex.encodeHexString(data)); } } else if (VERSION1 == version) { if (VERSION1_LEN != data.length) { throw new IllegalArgumentException("Invalid version one DLSN " + Hex.encodeHexString(data)); } } else { throw new IllegalArgumentException( "Invalid DLSN : version = " + version + ", " + Hex.encodeHexString(data)); } return new DLSN(bb.getLong(), bb.getLong(), bb.getLong()); }
From source file:org.bimserver.webservices.authorization.Authorization.java
public static Authorization fromToken(SecretKeySpec key, String token) throws AuthenticationException { if (token == null) { throw new IllegalArgumentException("Token required"); }// w w w .j a va 2 s . c o m try { int hashSizeBytes = 16; Cipher decodingCipher = Cipher.getInstance("AES"); decodingCipher.init(Cipher.DECRYPT_MODE, key); ByteBuffer buffer = ByteBuffer.wrap(decodingCipher.doFinal(Hex.decodeHex(token.toCharArray()))); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] foundHash = new byte[hashSizeBytes]; buffer.get(foundHash, 0, hashSizeBytes); byte[] hashInput = new byte[buffer.capacity() - hashSizeBytes]; buffer.get(hashInput); buffer.position(hashSizeBytes); byte[] calculatedHash = messageDigest.digest(hashInput); if (Arrays.equals(foundHash, calculatedHash)) { byte type = buffer.get(); Authorization authorization = null; long expires = buffer.getLong(); long uoid = buffer.getLong(); switch (type) { case ExplicitRightsAuthorization.ID: authorization = ExplicitRightsAuthorization.fromBuffer(buffer); break; case UserAuthorization.ID: authorization = UserAuthorization.fromBuffer(buffer); break; case SystemAuthorization.ID: authorization = SystemAuthorization.fromBuffer(buffer); break; case AnonymousAuthorization.ID: authorization = AnonymousAuthorization.fromBuffer(buffer); break; case AdminAuthorization.ID: authorization = AdminAuthorization.fromBuffer(buffer); break; case SingleProjectAuthorization.ID: authorization = SingleProjectAuthorization.fromBuffer(buffer); break; default: throw new AuthenticationException("Unknown authorization type: " + type); } authorization.setUoid(uoid); authorization.setExpires(expires); if (authorization.getExpires().getTimeInMillis() < new GregorianCalendar().getTimeInMillis()) { throw new AuthenticationException("This token has expired"); } return authorization; } else { throw new AuthenticationException("Given token is corrupt"); } } catch (GeneralSecurityException e) { throw new AuthenticationException("Invalid token", e); } catch (DecoderException e) { throw new AuthenticationException(e); } }
From source file:org.apache.hadoop.hbase.filter.SlicedRowFilter.java
public static SlicedRowFilter parseFrom(final byte[] pbBytes) throws DeserializationException { ByteBuffer bb = ByteBuffer.wrap(pbBytes).order(ByteOrder.BIG_ENDIAN); SlicedRowFilter filter = new SlicedRowFilter(); filter.count = bb.getLong(); filter.slicesLength = bb.getInt();//from ww w. ja v a2 s . c om int nbounds = bb.getInt(); filter.bounds = new int[nbounds]; for (int i = 0; i < nbounds; i++) { filter.bounds[i] = bb.getInt(); } // // If the first slice starts at offset 0 then we will be able to provide a key hint // if (0 == filter.bounds[0]) { filter.hasHinting = true; } else { filter.hasHinting = false; } filter.rangekeys = new byte[bb.getInt()]; bb.get(filter.rangekeys); filter.slice = new byte[filter.slicesLength]; return filter; }
From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java
/** * Tries to read an existing index from the given tar file. The index is * returned if it is found and looks valid (correct checksum, passes * sanity checks)./*from w ww . ja va2 s . c o m*/ * * @param file tar file * @param name name of the tar file, for logging purposes * @return tar index, or {@code null} if not found or not valid * @throws IOException if the tar file could not be read */ private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException { long length = file.length(); if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) { log.warn("Unexpected size {} of tar file {}", length, name); return null; // unexpected file size } // read the index metadata just before the two final zero blocks ByteBuffer meta = ByteBuffer.allocate(16); file.seek(length - 2 * BLOCK_SIZE - 16); file.readFully(meta.array()); int crc32 = meta.getInt(); int count = meta.getInt(); int bytes = meta.getInt(); int magic = meta.getInt(); if (magic != INDEX_MAGIC) { return null; // magic byte mismatch } if (count < 1 || bytes < count * 24 + 16 || bytes % BLOCK_SIZE != 0) { log.warn("Invalid index metadata in tar file {}", name); return null; // impossible entry and/or byte counts } // this involves seeking backwards in the file, which might not // perform well, but that's OK since we only do this once per file ByteBuffer index = ByteBuffer.allocate(count * 24); file.seek(length - 2 * BLOCK_SIZE - 16 - count * 24); file.readFully(index.array()); index.mark(); CRC32 checksum = new CRC32(); long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE; long lastmsb = Long.MIN_VALUE; long lastlsb = Long.MIN_VALUE; byte[] entry = new byte[24]; for (int i = 0; i < count; i++) { index.get(entry); checksum.update(entry); ByteBuffer buffer = ByteBuffer.wrap(entry); long msb = buffer.getLong(); long lsb = buffer.getLong(); int offset = buffer.getInt(); int size = buffer.getInt(); if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) { log.warn("Incorrect index ordering in tar file {}", name); return null; } else if (lastmsb == msb && lastlsb == lsb && i > 0) { log.warn("Duplicate index entry in tar file {}", name); return null; } else if (offset < 0 || offset % BLOCK_SIZE != 0) { log.warn("Invalid index entry offset in tar file {}", name); return null; } else if (size < 1 || offset + size > limit) { log.warn("Invalid index entry size in tar file {}", name); return null; } lastmsb = msb; lastlsb = lsb; } if (crc32 != (int) checksum.getValue()) { log.warn("Invalid index checksum in tar file {}", name); return null; // checksum mismatch } index.reset(); return index; }
From source file:org.apache.kylin.metadata.datatype.BooleanSerializer.java
@Override public Long deserialize(ByteBuffer in) { return in.getLong(); }
From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java
/** * Tries to read an existing index from the given tar file. The index is * returned if it is found and looks valid (correct checksum, passes * sanity checks).// w w w .ja v a2 s . c o m * * @param file tar file * @param name name of the tar file, for logging purposes * @return tar index, or {@code null} if not found or not valid * @throws IOException if the tar file could not be read */ private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException { long length = file.length(); if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) { log.warn("Unexpected size {} of tar file {}", length, name); return null; // unexpected file size } // read the index metadata just before the two final zero blocks ByteBuffer meta = ByteBuffer.allocate(16); file.seek(length - 2 * BLOCK_SIZE - 16); file.readFully(meta.array()); int crc32 = meta.getInt(); int count = meta.getInt(); int bytes = meta.getInt(); int magic = meta.getInt(); if (magic != INDEX_MAGIC) { return null; // magic byte mismatch } if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) { log.warn("Invalid index metadata in tar file {}", name); return null; // impossible entry and/or byte counts } // this involves seeking backwards in the file, which might not // perform well, but that's OK since we only do this once per file ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE); file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE); file.readFully(index.array()); index.mark(); CRC32 checksum = new CRC32(); long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE; long lastmsb = Long.MIN_VALUE; long lastlsb = Long.MIN_VALUE; byte[] entry = new byte[TarEntry.SIZE]; for (int i = 0; i < count; i++) { index.get(entry); checksum.update(entry); ByteBuffer buffer = wrap(entry); long msb = buffer.getLong(); long lsb = buffer.getLong(); int offset = buffer.getInt(); int size = buffer.getInt(); if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) { log.warn("Incorrect index ordering in tar file {}", name); return null; } else if (lastmsb == msb && lastlsb == lsb && i > 0) { log.warn("Duplicate index entry in tar file {}", name); return null; } else if (offset < 0 || offset % BLOCK_SIZE != 0) { log.warn("Invalid index entry offset in tar file {}", name); return null; } else if (size < 1 || offset + size > limit) { log.warn("Invalid index entry size in tar file {}", name); return null; } lastmsb = msb; lastlsb = lsb; } if (crc32 != (int) checksum.getValue()) { log.warn("Invalid index checksum in tar file {}", name); return null; // checksum mismatch } index.reset(); return index; }
From source file:org.dashbuilder.dataset.UUIDGeneratorImpl.java
public String uuidFromBase64(String str) { byte[] bytes = Base64.decodeBase64(str); ByteBuffer bb = ByteBuffer.wrap(bytes); UUID uuid = new UUID(bb.getLong(), bb.getLong()); return uuid.toString(); }
From source file:org.dashbuilder.dataset.backend.BackendUUIDGenerator.java
public String uuidFromBase64(String str) { Base64 base64 = new Base64(); byte[] bytes = base64.decodeBase64(str); ByteBuffer bb = ByteBuffer.wrap(bytes); UUID uuid = new UUID(bb.getLong(), bb.getLong()); return uuid.toString(); }