List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:guru.benson.pinch.Pinch.java
/** * Extract all ZipEntries from the ZIP central directory. * * @param buf/*from ww w .ja va 2 s .c om*/ * The byte buffer containing the ZIP central directory. * * @return A list with all ZipEntries. */ private static ArrayList<ExtendedZipEntry> parseHeaders(ByteBuffer buf) { ArrayList<ExtendedZipEntry> zeList = new ArrayList<ExtendedZipEntry>(); buf.order(ByteOrder.LITTLE_ENDIAN); int offset = 0; while (offset < buf.limit() - ZipConstants.CENHDR) { short fileNameLen = buf.getShort(offset + ZipConstants.CENNAM); short extraFieldLen = buf.getShort(offset + ZipConstants.CENEXT); short fileCommentLen = buf.getShort(offset + ZipConstants.CENCOM); String fileName = new String(buf.array(), offset + ZipConstants.CENHDR, fileNameLen); ExtendedZipEntry zeGermans = new ExtendedZipEntry(fileName); zeGermans.setMethod(buf.getShort(offset + ZipConstants.CENHOW)); CRC32 crc = new CRC32(); crc.update(buf.getInt(offset + ZipConstants.CENCRC)); zeGermans.setCrc(crc.getValue()); zeGermans.setCompressedSize(buf.getInt(offset + ZipConstants.CENSIZ)); zeGermans.setSize(buf.getInt(offset + ZipConstants.CENLEN)); zeGermans.setInternalAttr(buf.getShort(offset + ZipConstants.CENATT)); zeGermans.setExternalAttr(buf.getShort(offset + ZipConstants.CENATX)); zeGermans.setOffset((long) buf.getInt(offset + ZipConstants.CENOFF)); zeGermans.setExtraLength(extraFieldLen); zeList.add(zeGermans); offset += ZipConstants.CENHDR + fileNameLen + extraFieldLen + fileCommentLen; } return zeList; }
From source file:com.igormaznitsa.jcp.utils.PreprocessorUtils.java
@Nonnull public static byte[] readFileAsByteArray(@Nonnull final File file) throws IOException { checkFile(file);/*from ww w .ja v a 2 s. c om*/ int len = (int) file.length(); final ByteBuffer buffer = ByteBuffer.allocate(len); final FileChannel inChannel = new FileInputStream(file).getChannel(); try { while (len > 0) { final int read = inChannel.read(buffer); if (read < 0) { throw new IOException("Can't read whole file [" + getFilePath(file) + '\''); } len -= read; } } finally { IOUtils.closeQuietly(inChannel); } return buffer.array(); }
From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java
private static byte[] addWavHeader(byte[] bytes) throws IOException { ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44); bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN); bufferWithHeader.put("RIFF".getBytes()); bufferWithHeader.putInt(bytes.length + 36); bufferWithHeader.put("WAVE".getBytes()); bufferWithHeader.put("fmt ".getBytes()); bufferWithHeader.putInt(16);// w ww. ja va2 s .c om bufferWithHeader.putShort((short) 1); bufferWithHeader.putShort((short) 1); bufferWithHeader.putInt(16000); bufferWithHeader.putInt(32000); bufferWithHeader.putShort((short) 2); bufferWithHeader.putShort((short) 16); bufferWithHeader.put("data".getBytes()); bufferWithHeader.putInt(bytes.length); bufferWithHeader.put(bytes); return bufferWithHeader.array(); }
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * readFile//from ww w .j av a 2s .com * * @param filePath ? * @return byte[] */ public static byte[] readFile(String filePath) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "readFile", "[filePath:" + filePath + "]")); if (StringUtils.isEmpty(filePath)) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]")); throw new IllegalArgumentException(); } File file = new File(filePath); FileInputStream fin = null; FileChannel fcin = null; ByteBuffer rbuffer = ByteBuffer.allocate(TesseractConstant.FILE_BLOCK_SIZE); ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (file.exists()) { try { fin = new FileInputStream(file); fcin = fin.getChannel(); while (fcin.read(rbuffer) != -1) { bos.write(rbuffer.array()); } } catch (Exception e) { LOGGER.error(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]"), e); } finally { try { if (fin != null) { fin.close(); } if (fcin != null) { fcin.close(); } bos.close(); } catch (Exception e) { LOGGER.error(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]"), e); } } } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "readFile", "[filePath:" + filePath + "]")); return bos.toByteArray(); }
From source file:net.phoenix.thrift.hello.SpringHelloService.java
@Override public ByteBuffer testBinary(ByteBuffer name) throws TException { try {//from w ww . j av a 2s . c o m String result = new String(name.array(), name.position(), name.limit() - name.position(), "UTF-8"); result = "Hello " + result; return ByteBuffer.wrap(result.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new TException(e); } }
From source file:eu.pursuit.core.ScopeID.java
public byte[] toByteArray() { ByteBuffer buffer = ByteBuffer.allocate(getLength()); fill(buffer); return buffer.array(); }
From source file:eu.stratosphere.arraymodel.io.StringInputFormat.java
public boolean readRecord(Value[] target, byte[] bytes, int offset, int numBytes) { StringValue str = this.theString; if (this.ascii) { str.setValueAscii(bytes, offset, numBytes); } else {// w w w .j av a 2 s.co m ByteBuffer byteWrapper = this.byteWrapper; if (bytes != byteWrapper.array()) { byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length); this.byteWrapper = byteWrapper; } byteWrapper.clear(); byteWrapper.position(offset); byteWrapper.limit(offset + numBytes); try { CharBuffer result = this.decoder.decode(byteWrapper); str.setValue(result); } catch (CharacterCodingException e) { byte[] copy = new byte[numBytes]; System.arraycopy(bytes, offset, copy, 0, numBytes); LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e); return false; } } target[0] = str; return true; }
From source file:eu.stratosphere.pact.array.io.StringInputFormat.java
public boolean readRecord(Value[] target, byte[] bytes, int offset, int numBytes) { PactString str = this.theString; if (this.ascii) { str.setValueAscii(bytes, offset, numBytes); } else {//from w w w . j a v a2s.co m ByteBuffer byteWrapper = this.byteWrapper; if (bytes != byteWrapper.array()) { byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length); this.byteWrapper = byteWrapper; } byteWrapper.clear(); byteWrapper.position(offset); byteWrapper.limit(offset + numBytes); try { CharBuffer result = this.decoder.decode(byteWrapper); str.setValue(result); } catch (CharacterCodingException e) { byte[] copy = new byte[numBytes]; System.arraycopy(bytes, offset, copy, 0, numBytes); LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e); return false; } } target[0] = str; return true; }
From source file:me.whitmarbut.mfa.TOTP.java
private byte[] getHmac(int timestamp, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec key_spec = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key_spec);/*from w w w .j a va2 s. c om*/ byte[] bin_timestamp = ByteBuffer.allocate(4).putInt(timestamp).array(); ByteBuffer bbuff = ByteBuffer.allocate(8); bbuff.putInt(0); //Left pad 4 bytes to make a 64 bit int bbuff.putInt(timestamp); return mac.doFinal(bbuff.array()); }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * from to /*from w w w. j ava2 s . c o m*/ * * @param fromBuffer * Buffer ? flush * @param toBuffer * Buffer ? fill * @return number of bytes moved */ public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) { int put; int remaining = fromBuffer.remaining(); if (remaining > 0) { // if (remaining <= toBuffer.remaining()) { toBuffer.put(fromBuffer); put = remaining; // from fromBuffer.position(fromBuffer.limit()); } // heap buffer else if (fromBuffer.hasArray()) { put = toBuffer.remaining(); // ?? toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put); fromBuffer.position(fromBuffer.position() + put); } // direct buffer else { // ?? put = toBuffer.remaining(); ByteBuffer slice = fromBuffer.slice(); slice.limit(put); toBuffer.put(slice); fromBuffer.position(fromBuffer.position() + put); } } else { put = 0; } return put; }