List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:com.github.ambry.commons.BlobId.java
@Override public byte[] toBytes() { ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes()); idBuf.putShort(version);/*from www . j a v a 2 s . c o m*/ idBuf.put(partitionId.getBytes()); idBuf.putInt(uuid.getBytes().length); idBuf.put(uuid.getBytes()); return idBuf.array(); }
From source file:com.ebay.pulsar.analytics.metricstore.druid.query.model.TopNQuery.java
@Override public byte[] cacheKey() { byte[] dimensionSpecBytes = this.getDimension().getBytes(); byte[] metricSpecBytes = this.getMetric().cacheKey(); BaseFilter filter = this.getFilter(); byte[] filterBytes = filter == null ? new byte[] {} : filter.cacheKey(); byte[] aggregatorBytes = QueryCacheHelper.computeAggregatorBytes(this.getAggregations()); byte[] granularityBytes = null; if (this.getGranularity() instanceof BaseGranularity) { granularityBytes = ((BaseGranularity) this.getGranularity()).cacheKey(); } else {/* w w w. ja v a 2 s. co m*/ granularityBytes = new byte[0]; } byte[] intervalsBytes = QueryCacheHelper.computeIntervalsBytes(this.getIntervals()); List<BasePostAggregator> postaggregators = this.getPostAggregations(); byte[] postaggregatorBytes = postaggregators == null ? new byte[] {} : QueryCacheHelper.computePostAggregatorBytes(postaggregators); return ByteBuffer .allocate(1 + dimensionSpecBytes.length + metricSpecBytes.length + 4 + granularityBytes.length + filterBytes.length + aggregatorBytes.length + intervalsBytes.length + postaggregatorBytes.length) .put(TOPN_QUERY).put(dimensionSpecBytes).put(metricSpecBytes) .put(Ints.toByteArray(this.getThreshold())).put(granularityBytes).put(filterBytes) .put(aggregatorBytes).put(intervalsBytes).put(postaggregatorBytes).array(); }
From source file:com.boylesoftware.web.impl.auth.CipherToolbox.java
/** * Create new instance./*from w w w. j av a 2 s . c o m*/ * * @param pool Reference to the owning pool. * @param pooledObjectId Pooled object id. * @param secretKey The key. */ CipherToolbox(final FastPool<CipherToolbox> pool, final int pooledObjectId, final Key secretKey) { super(pool, pooledObjectId); this.secretKey = secretKey; try { this.cipher = Cipher.getInstance(ALGORITHM); } catch (final GeneralSecurityException e) { throw new RuntimeException("Error creating cipher.", e); } this.clearBuf = ByteBuffer.allocate(48); this.cipherBuf = ByteBuffer.allocate(48); this.base64Chars = new char[64]; this.base64Buf = CharBuffer.wrap(this.base64Chars); }
From source file:com.torchmind.authenticator.AbstractTokenGenerator.java
/** * Generates a code based on a secret key and challenge. * * @param secretKey a secret key./*from w w w. j a v a 2s . c o m*/ * @param challenge a challenge. * @return a code. */ @NonNull protected String generateCode(@NonNull SecretKey secretKey, @NonNull byte[] challenge) { try { Mac mac = Mac.getInstance("Hmac" + this.algorithm.name()); mac.init(secretKey); byte[] hash = mac.doFinal(challenge); int offset = hash[hash.length - 1] & 0x0F; ByteBuffer buffer = ByteBuffer.allocate(4).put(hash, offset, 4); buffer.flip(); return String.format("%0" + this.digits + "d", (buffer.getInt() & 0x7FFFFFFF) % this.digitModulo); } catch (NoSuchAlgorithmException ex) { throw new UnsupportedOperationException( "The specified algorithm is not supported by this Java VM implementation: " + ex.getMessage(), ex); } catch (InvalidKeyException ex) { throw new IllegalArgumentException("Invalid shared secret: " + ex.getMessage(), ex); } }
From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java
public void write(OutputStream output) throws IOException { baseBuffer.rewind();//from w ww. j a v a 2 s . c om checkBuffer.rewind(); tailBuffer.rewind(); int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity()); int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity()); DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output)); dataOutput.writeBoolean(compact); dataOutput.writeInt(baseCheckSize); dataOutput.writeInt(tailSize); WritableByteChannel channel = Channels.newChannel(dataOutput); ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4); IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer(); tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize); tmpBuffer.rewind(); channel.write(tmpBuffer); tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4); tmpIntBuffer = tmpBuffer.asIntBuffer(); tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize); tmpBuffer.rewind(); channel.write(tmpBuffer); tmpBuffer = ByteBuffer.allocate(tailSize * 2); CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer(); tmpCharBuffer.put(tailBuffer.array(), 0, tailSize); tmpBuffer.rewind(); channel.write(tmpBuffer); dataOutput.flush(); }
From source file:com.buaa.cfs.nfs3.FileHandle.java
private long bytesToLong(byte[] data) { ByteBuffer buffer = ByteBuffer.allocate(8); for (int i = 0; i < 8; i++) { buffer.put(data[i]);//from w w w.j a v a 2 s . c o m } buffer.flip();// need flip return buffer.getLong(); }
From source file:ConversionUtil.java
public static byte[] convertToByteArray(float value) { byte[] bytes = new byte[4]; ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.putFloat(value);/*from www . ja v a 2 s. co m*/ return buffer.array(); // buffer.get(bytes); // return bytes; // return convertToByteArray(Float.floatToIntBits(value)); }
From source file:com.log4ic.compressor.utils.FileUtils.java
/** * ?/*ww w .ja v a 2 s . c o m*/ * * @param fileInputStream * @return */ public static String readFile(FileInputStream fileInputStream) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); StringBuffer contentBuffer = new StringBuffer(); Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { FileChannel channel = fileInputStream.getChannel(); while (true) { buffer.clear(); int pos = channel.read(buffer); if (pos == -1) { break; } buffer.flip(); charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); contentBuffer.append(charBuffer.toString()); } } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return contentBuffer.toString(); }
From source file:com.sastix.cms.server.services.content.impl.ZipFileHandlerServiceImpl.java
@Override public DataMaps unzip(byte[] bytes) throws IOException { Map<String, String> foldersMap = new HashMap<>(); Map<String, byte[]> extractedBytesMap = new HashMap<>(); InputStream byteInputStream = new ByteArrayInputStream(bytes); //validate that it is a zip file if (isZipFile(bytes)) { try {/* www . j ava 2 s .c o m*/ //get the zip file content ZipInputStream zis = new ZipInputStream(byteInputStream); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); if (!ze.isDirectory()) {//if entry is a directory, we should not add it as a file ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ByteBuffer bufIn = ByteBuffer.allocate(1024); int bytesRead; while ((bytesRead = zis.read(bufIn.array())) > 0) { baos.write(bufIn.array(), 0, bytesRead); bufIn.rewind(); } bufIn.clear(); extractedBytesMap.put(fileName, baos.toByteArray()); } finally { baos.close(); } } else { foldersMap.put(fileName, fileName); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { ex.printStackTrace(); } } DataMaps dataMaps = new DataMaps(); dataMaps.setBytesMap(extractedBytesMap); dataMaps.setFoldersMap(foldersMap); return dataMaps; }
From source file:info.varden.irclinqed.dcc.FileReceiveThread.java
@Override public void run() { try {/*w w w. j a va 2 s . com*/ this.il.keyListenerQueue.add(this); this.gfp = new GuiFileProgress(this.packet.il, this); this.packet.il.guiQueue.add(this.gfp); if (!this.file.getParentFile().exists()) { this.file.getParentFile().mkdirs(); } if (!this.file.exists()) { this.file.createNewFile(); } Socket s = new Socket(this.host, this.port); InputStream i = s.getInputStream(); this.cos = new CountingOutputStream(new FileOutputStream(this.file)); byte[] buff = new byte[1024]; int k = -1; while ((k = i.read(buff)) > -1 && !this.cancel) { this.cos.write(buff, 0, k); s.getOutputStream().write(ByteBuffer.allocate(4).putInt((int) getByteCount()).array()); } s.shutdownInput(); s.shutdownOutput(); s.close(); this.cos.close(); this.gfp.unload(); } catch (IOException e) { e.printStackTrace(); } }