List of usage examples for java.nio ByteBuffer wrap
public static ByteBuffer wrap(byte[] array)
From source file:io.anserini.embeddings.WordEmbeddingDictionary.java
public float[] getEmbeddingVector(String term) throws IOException { Query query = AnalyzerUtils.buildBagOfWordsQuery(FIELD_ID, analyzer, term); TopDocs rs = searcher.search(query, 1); ScoredDocuments docs = ScoredDocuments.fromTopDocs(rs, searcher); byte[] val = docs.documents[0].getField(FIELD_BODY).binaryValue().bytes; FloatBuffer floatBuffer = ByteBuffer.wrap(val).asFloatBuffer(); float[] floatArray = new float[floatBuffer.limit()]; floatBuffer.get(floatArray);/*from w w w . j a v a 2 s .c om*/ return floatArray; }
From source file:com.mastercard.walletservices.mdes.DeviceInfo.java
/** * Return ByteArray of device finger print. * * @return The Device Finger Pring as String *///from ww w . jav a 2 s. c o m public String getDeviceFingerprint() { byte[] dataBytes = (this.deviceName + this.deviceType + this.imei + this.msisdn + this.nfcCapable + this.osName).getBytes(); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[dataBytes.length]); byteBuffer.put(dataBytes); // Create MessageDigest using SHA-256 algorithm Added required MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } // Hash the result byte[] hash = messageDigest.digest(byteBuffer.array()); // Return Hex return new String(Hex.encodeHex(hash)); }
From source file:com.rackspacecloud.blueflood.io.serializers.EnumRollupSerializationTest.java
@Test public void testEnumV1RoundTrip() throws IOException { BluefloodEnumRollup e0 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L) .withEnumValue("enumValue2", 5L); BluefloodEnumRollup e1 = new BluefloodEnumRollup().withEnumValue( "t4.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met", 34454722343L)//from w ww.ja v a 2 s . c om .withEnumValue( "t5.enum.abcdefg.hijklmnop.qrstuvw.xyz.ABCDEFG.HIJKLMNOP.QRSTUVW.XYZ.abcdefg.hijklmnop.qrstuvw.xyz.met", 34454722343L) .withEnumValue("enumValue2", 10L); BluefloodEnumRollup e2 = new BluefloodEnumRollup().withEnumValue("enumValue1", 1L) .withEnumValue("enumValue1", 1L); BluefloodEnumRollup er = BluefloodEnumRollup .buildRollupFromEnumRollups(Rollups.asPoints(BluefloodEnumRollup.class, 0, 300, e0, e1, e2)); Assert.assertEquals(4, er.getCount()); Map<Long, Long> map = er.getHashedEnumValuesWithCounts(); Assert.assertTrue(map.get((long) "enumValue1".hashCode()) == 3L); Assert.assertTrue(map.get((long) "enumValue2".hashCode()) == 15L); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e0).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e1).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(e2).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(new NumericSerializer.EnumRollupSerializer().toByteBuffer(er).array())); baos.write("\n".getBytes()); baos.close(); BufferedReader reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodEnumRollup ee0 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb); Assert.assertEquals(e0, ee0); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodEnumRollup ee1 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb); Assert.assertEquals(e1, ee1); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodEnumRollup ee2 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb); Assert.assertEquals(e2, ee2); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodEnumRollup ee3 = NumericSerializer.serializerFor(BluefloodEnumRollup.class).fromByteBuffer(bb); Assert.assertEquals(er, ee3); Assert.assertFalse(ee0.equals(ee1)); }
From source file:com.knewton.mapreduce.example.StudentEventMapperTest.java
/** * Test the start time range filters in the mapper. *//*w ww.ja v a 2 s .c o m*/ @Test public void testMapWithStartRangeStudentEvents() throws Exception { conf.set(PropertyConstants.START_DATE.txt, "2013-03-28T23:03:02.394Z"); underTest.setup(mockedContext); // send event outside of time range // Mar.28.2013.19:53:38.0.0 DateTime dt = new DateTime(2013, 3, 28, 19, 53, 10, DateTimeZone.UTC); long eventId = dt.getMillis(); ByteBuffer randomKey = RandomStudentEventGenerator.getRandomIdBuffer(); ByteBuffer columnName = ByteBuffer.wrap(new byte[8]); columnName.putLong(eventId); columnName.rewind(); CellName cellName = simpleDenseCellType.cellFromByteBuffer(columnName); Cell column = new BufferCell(cellName, ByteBuffer.wrap(Hex.decodeHex(eventDataString.toCharArray()))); underTest.map(randomKey, column, mockedContext); verify(mockedContext, never()).write(any(LongWritable.class), any(StudentEventWritable.class)); // send event inside of time range // Mar.29.2013.03:07:21.0.0 dt = new DateTime(2013, 3, 29, 3, 7, 21, DateTimeZone.UTC); eventId = dt.getMillis(); columnName = ByteBuffer.wrap(new byte[8]); columnName.putLong(eventId); columnName.rewind(); cellName = simpleDenseCellType.cellFromByteBuffer(columnName); column = new BufferCell(cellName, ByteBuffer.wrap(Hex.decodeHex(eventDataString.toCharArray()))); randomKey = RandomStudentEventGenerator.getRandomIdBuffer(); underTest.map(randomKey, column, mockedContext); verify(mockedContext).write(any(LongWritable.class), any(StudentEventWritable.class)); }
From source file:net.phoenix.thrift.hello.ThreadPoolTest.java
@Test public void testBinary() throws Exception { LOG.info("Client starting...."); String name = "Hello "; // TTransport transport = new TNonblockingSocket("localhost", 9804); TTransport transport = new TSocket("localhost", 9804); transport.open();/*w ww. j a va 2 s. co m*/ // TTransport transport = new TTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); try { ByteBuffer buffer = client.testBinary(ByteBuffer.wrap(name.getBytes("UTF-8"))); assertEquals(new String(buffer.array(), buffer.position(), buffer.limit() - buffer.position(), "UTF-8"), "Hello " + name); // System.out.println(message); } finally { transport.close(); } }
From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java
@SuppressWarnings("unused") public static byte[] toBytes(final UUID uuid) { final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); return byteBuffer.array(); }
From source file:com.palantir.atlasdb.keyvalue.rocksdb.impl.RocksDbKeyValueServices.java
static byte[] getKey(byte[] row, byte[] col, long timeStamp) { Preconditions.checkArgument(EncodingUtils.sizeOfVarLong(row.length) <= 2); byte[] rowSize = EncodingUtils.encodeVarLong(row.length); ArrayUtils.reverse(rowSize);//from w ww. j a v a 2 s. c om byte[] key = new byte[row.length + col.length + 8 + rowSize.length]; ByteBuffer.wrap(key).order(ByteOrder.BIG_ENDIAN).put(row).put(col).putLong(timeStamp).put(rowSize); return key; }
From source file:edu.umn.cs.spatialHadoop.core.OGCESRIShape.java
@Override public void readFields(DataInput in) throws IOException { int size = in.readInt(); byte[] bytes = new byte[size]; in.readFully(bytes);// w ww. j av a 2 s. c o m geom = OGCGeometry.fromBinary(ByteBuffer.wrap(bytes)); }
From source file:tanks10.SendAndReceive.java
public void handleMessage(String msg) { try {//from w w w. ja va 2s .co m byte[] backingArray = msg.getBytes("UTF-8"); ByteBuffer readBuffer = ByteBuffer.wrap(backingArray); protocol.receive(readBuffer); } catch (UnsupportedEncodingException ex) { LOG.log(Level.SEVERE, "message parsing failed for: " + session.getRemoteAddress().toString() + " msg:" + msg, ex); } }
From source file:io.github.dsheirer.record.wave.MonoWaveReader.java
/** * Opens the file/* w w w. jav a 2 s. c o m*/ */ private void open() throws IOException { if (!Files.exists(mPath)) { throw new IOException("File not found"); } mInputStream = Files.newInputStream(mPath, StandardOpenOption.READ); //Check for RIFF header byte[] buffer = new byte[4]; mInputStream.read(buffer); if (!Arrays.equals(buffer, WaveUtils.RIFF_CHUNK)) { throw new IOException("File is not .wav format - missing RIFF chunk"); } //Get file size mInputStream.read(buffer); int fileSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(); //Check for WAVE format mInputStream.read(buffer); if (!Arrays.equals(buffer, WaveUtils.WAV_FORMAT)) { throw new IOException("File is not .wav format - missing WAVE format"); } //Check for format chunk mInputStream.read(buffer); if (!Arrays.equals(buffer, WaveUtils.CHUNK_FORMAT)) { throw new IOException("File is not .wav format - missing format chunk"); } //Get chunk size mInputStream.read(buffer); int chunkSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(); //Get format mInputStream.read(buffer); ShortBuffer shortBuffer = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); short format = shortBuffer.get(); if (format != WaveUtils.PCM_FORMAT) { throw new IOException("File format not supported - expecting PCM format"); } //Get number of channels short channels = shortBuffer.get(); if (channels != 1) { throw new IOException("Unsupported channel count - mono audio only"); } //Get samples per second mInputStream.read(buffer); int sampleRate = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(); //Get bytes per second mInputStream.read(buffer); int bytesPerSecond = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(); mInputStream.read(buffer); //Get frame size shortBuffer = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); short frameSize = shortBuffer.get(); if (frameSize != 2) { throw new IOException("PCM frame size not supported - expecting 2 bytes per frame"); } //Get bits per sample short bitsPerSample = shortBuffer.get(); if (bitsPerSample != 16) { throw new IOException("PCM sample size not supported - expecting 16 bits per sample"); } mInputStream.read(buffer); if (!Arrays.equals(buffer, WaveUtils.CHUNK_DATA)) { throw new IOException("Unexpected chunk - expecting data chunk"); } //Get data chunk size mInputStream.read(buffer); mDataByteSize = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(); }