List of usage examples for java.nio ByteBuffer wrap
public static ByteBuffer wrap(byte[] array)
From source file:NGzipCompressingEntity.java
public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException { if (baos == null) { baos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(baos); InputStream in = wrappedEntity.getContent(); byte[] tmp = new byte[2048]; int l;/* www . j a va 2 s . c o m*/ while ((l = in.read(tmp)) != -1) { gzip.write(tmp, 0, l); } gzip.close(); buffer = ByteBuffer.wrap(baos.toByteArray()); } encoder.write(buffer); if (!buffer.hasRemaining()) encoder.complete(); }
From source file:com.amazonaws.services.kinesis.application.stocktrades.writer.StockTradesWriter.java
/** * Uses the Kinesis client to send the stock trade to the given stream. * * @param trade instance representing the stock trade * @param kinesisClient Amazon Kinesis client * @param streamName Name of stream//from w ww . j a va 2 s . c om */ private static void sendStockTrade(StockTrade trade, AmazonKinesis kinesisClient, String streamName) { byte[] bytes = trade.toJsonAsBytes(); // The bytes could be null if there is an issue with the JSON serialization by the Jackson JSON library. if (bytes == null) { LOG.warn("Could not get JSON bytes for stock trade"); return; } LOG.info("Putting trade: " + trade.toString()); PutRecordRequest putRecord = new PutRecordRequest(); putRecord.setStreamName(streamName); // We use the ticker symbol as the partition key, as explained in the tutorial. putRecord.setPartitionKey(trade.getTickerSymbol()); putRecord.setData(ByteBuffer.wrap(bytes)); try { kinesisClient.putRecord(putRecord); } catch (AmazonClientException ex) { LOG.warn("Error sending record to Amazon Kinesis.", ex); } }
From source file:com.rapleaf.jack.ModelWithId.java
public boolean equals(ModelWithId obj) { if (obj == null) return false; if (!this.getClass().getName().equals(obj.getClass().getName())) { return false; }// ww w.java2 s .com if (getId() != obj.getId()) { return false; } for (Enum field : getFieldSet()) { Object value1 = getField(field.name()); Object value2 = obj.getField(field.name()); if (value1 != null) { if (value1 instanceof byte[]) { value1 = ByteBuffer.wrap((byte[]) value1); value2 = ByteBuffer.wrap((byte[]) value2); } if (!value1.equals(value2)) { return false; } } else { if (value2 != null) { return false; } } } return true; }
From source file:de.brendamour.jpasskit.signing.PKInMemorySigningUtil.java
@Override public byte[] createSignedAndZippedPkPassArchive(PKPass pass, IPKPassTemplate passTemplate, PKSigningInformation signingInformation) throws PKSigningException { Map<String, ByteBuffer> allFiles; try {//from w ww . j a v a 2s . c om allFiles = passTemplate.getAllFiles(); } catch (IOException e) { throw new PKSigningException("Error when getting files from template", e); } ByteBuffer passJSONFile = createPassJSONFile(pass); allFiles.put(PASS_JSON_FILE_NAME, passJSONFile); ByteBuffer manifestJSONFile = createManifestJSONFile(allFiles); allFiles.put(MANIFEST_JSON_FILE_NAME, manifestJSONFile); ByteBuffer signature = ByteBuffer.wrap(signManifestFile(manifestJSONFile.array(), signingInformation)); allFiles.put(SIGNATURE_FILE_NAME, signature); return createZippedPassAndReturnAsByteArray(allFiles); }
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 w w . j a v 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:tachyon.master.JsonObject.java
/** Deserializes a base64-encoded String as a ByteBuffer. */ public ByteBuffer getByteBuffer(String name) { String byteString = get(name, String.class); if (byteString == null) { return null; }/*from w w w . j a v a2 s. c om*/ return ByteBuffer.wrap(Base64.decodeBase64(byteString)); }
From source file:com.rackspacecloud.blueflood.io.SerializationTest.java
@Test public void testBadSerializationVersion() { byte[] buf = new byte[] { 99, 99 }; // hopefully we won't have 99 different serialization versions. for (Granularity g : Granularity.granularities()) { try {//w w w. jav a2 s .c o m NumericSerializer.get(g).fromByteBuffer(ByteBuffer.wrap(buf)); Assert.fail(String.format("Should have errored out %s", g.name())); } catch (RuntimeException ex) { Assert.assertTrue(ex.getCause().getMessage().startsWith("Unexpected serialization version")); } } }
From source file:jp.co.nemuzuka.service.impl.UploadFileServiceImpl.java
@Override public String put(FileItem fileItem, String comment, String ticketKeyToString, String projectKeyString) { BlobKey blobKey = null;//from ww w . j a v a 2s . c o m try { FileService fileService = FileServiceFactory.getFileService(); AppEngineFile blobFile = fileService.createNewBlobFile(fileItem.getContentType()); FileWriteChannel writeChannel = fileService.openWriteChannel(blobFile, true); writeChannel.write(ByteBuffer.wrap(fileItem.getData())); writeChannel.closeFinally(); blobKey = fileService.getBlobKey(blobFile); } catch (Exception e) { throw new RuntimeException(e); } //? UploadFileModel model = new UploadFileModel(); BlobInfoFactory factory = new BlobInfoFactory(); model.setBlobKey(blobKey.getKeyString()); BlobInfo blobInfo = factory.loadBlobInfo(blobKey); BeanUtil.copy(blobInfo, model, new CopyOptions().exclude("blobKey")); model.setParentKey(Datastore.stringToKey(ticketKeyToString)); model.setProjectKey(Datastore.stringToKey(projectKeyString)); model.setFilename(fileItem.getShortFileName()); model.setComment(new Text(StringUtils.defaultString(comment))); // Datastore.put(model); return Datastore.keyToString(model.getKey()); }
From source file:com.rackspacecloud.blueflood.io.serializers.astyanax.GaugeRollupSerializerTest.java
@Test public void testSerializerDeserializerV1() throws Exception { BluefloodGaugeRollup gauge1 = BluefloodGaugeRollup.buildFromRawSamples( Rollups.asPoints(SimpleNumber.class, System.currentTimeMillis(), 300, new SimpleNumber(10L))); BluefloodGaugeRollup gauge2 = BluefloodGaugeRollup.buildFromRawSamples(Rollups.asPoints(SimpleNumber.class, System.currentTimeMillis() - 100, 300, new SimpleNumber(1234567L))); BluefloodGaugeRollup gauge3 = BluefloodGaugeRollup.buildFromRawSamples(Rollups.asPoints(SimpleNumber.class, System.currentTimeMillis() - 200, 300, new SimpleNumber(10.4D))); BluefloodGaugeRollup gaugesRollup = BluefloodGaugeRollup.buildFromGaugeRollups(Rollups .asPoints(BluefloodGaugeRollup.class, System.currentTimeMillis(), 300, gauge1, gauge2, gauge3)); Assert.assertEquals(3, gaugesRollup.getCount()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(Base64.encodeBase64(Serializers.gaugeRollupInstance.toByteBuffer(gauge1).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(Serializers.gaugeRollupInstance.toByteBuffer(gauge2).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(Serializers.gaugeRollupInstance.toByteBuffer(gauge3).array())); baos.write("\n".getBytes()); baos.write(Base64.encodeBase64(Serializers.gaugeRollupInstance.toByteBuffer(gaugesRollup).array())); baos.write("\n".getBytes()); baos.close();// w w w.ja v a2 s . c o m BufferedReader reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodGaugeRollup deserializedGauge1 = Serializers.serializerFor(BluefloodGaugeRollup.class) .fromByteBuffer(bb); Assert.assertEquals(gauge1, deserializedGauge1); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodGaugeRollup deserializedGauge2 = Serializers.serializerFor(BluefloodGaugeRollup.class) .fromByteBuffer(bb); Assert.assertEquals(gauge2, deserializedGauge2); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodGaugeRollup deserializedGauge3 = Serializers.serializerFor(BluefloodGaugeRollup.class) .fromByteBuffer(bb); Assert.assertEquals(gauge3, deserializedGauge3); bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes())); BluefloodGaugeRollup deserializedGauge4 = Serializers.serializerFor(BluefloodGaugeRollup.class) .fromByteBuffer(bb); Assert.assertEquals(gaugesRollup, deserializedGauge4); Assert.assertFalse(deserializedGauge1.equals(deserializedGauge2)); }