List of usage examples for java.nio ByteBuffer hasArray
public final boolean hasArray()
From source file:org.apache.spark.network.util.JavaUtils.java
/** * Returns a byte array with the buffer's contents, trying to avoid copying the data if * possible.//from w w w. j a v a 2 s . c om */ public static byte[] bufferToArray(ByteBuffer buffer) { if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.array().length == buffer.remaining()) { return buffer.array(); } else { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); return bytes; } }
From source file:org.apache.tajo.storage.orc.OrcScanner.java
private static OrcProto.PostScript extractPostScript(ByteBuffer bb, Path path, int psLen, int psAbsOffset) throws IOException { // TODO: when PB is upgraded to 2.6, newInstance(ByteBuffer) method should be used here. assert bb.hasArray(); CodedInputStream in = CodedInputStream.newInstance(bb.array(), bb.arrayOffset() + psAbsOffset, psLen); OrcProto.PostScript ps = OrcProto.PostScript.parseFrom(in); checkOrcVersion(LOG, path, ps.getVersionList()); // Check compression codec. switch (ps.getCompression()) { case NONE://from w w w .j ava2s . co m break; case ZLIB: break; case SNAPPY: break; case LZO: break; default: throw new IllegalArgumentException("Unknown compression"); } return ps; }
From source file:org.kaaproject.kaa.server.common.utils.Utils.java
public static String encodeHexString(ByteBuffer buf) { return buf != null && buf.hasArray() ? Hex.encodeHexString(buf.array()) : ""; }
From source file:org.ms123.common.git.FileHolder.java
public static byte[] encode(final String str) { final ByteBuffer bb = Constants.CHARSET.encode(str); final int len = bb.limit(); if (bb.hasArray() && bb.arrayOffset() == 0) { final byte[] arr = bb.array(); if (arr.length == len) return arr; }/*ww w. j a v a 2s .c om*/ final byte[] arr = new byte[len]; bb.get(arr); return arr; }
From source file:org.panbox.desktop.common.vfs.backend.generic.GenericVirtualFileImpl.java
@Override public int write(long seekpos, ByteBuffer b) throws IOException, PanboxEncryptionException { try {/*ww w . j a v a2s.co m*/ aesRandomAccessFile.seek(seekpos); byte[] buf; if (b.hasArray()) { buf = b.array(); this.write(buf, 0, buf.length); } else { buf = new byte[b.remaining()]; b.get(buf); this.write(buf, 0, buf.length); } return buf.length; } catch (FileEncryptionException e) { throw new PanboxEncryptionException(e.getMessage(), e); } }
From source file:org.wso2.andes.mqtt.MQTTChannel.java
/** * Will add the message content which will be recived * * @param message the content of the message which was published * @param topic the name of the topic which the message was published * @param qosLevel the level of the qos the message was published * @param mqttLocalMessageID the channel id the subscriber is bound to * @param retain whether the message requires to be persisted * @param publisherID the id which will uniquely identify the publisher * @throws MQTTException occurs if there was an errro while adding the message content *///w w w . j a v a2s.c o m public void addMessage(ByteBuffer message, String topic, int qosLevel, int mqttLocalMessageID, boolean retain, UUID publisherID) throws MQTTException { if (message.hasArray()) { //Will get the bytes of the message byte[] messageData = message.array(); long messageID = 0; // unique message Id will be generated By Andes. //Will start converting the message body AndesMessagePart messagePart = MQTTUtils.convertToAndesMessage(messageData, messageID); //Will Create the Andes Header AndesMessageMetadata messageHeader = MQTTUtils.convertToAndesHeader(messageID, topic, qosLevel, messageData.length, retain, publisherID); AndesMessage andesMessage = new AndesMessage(messageHeader); andesMessage.addMessagePart(messagePart); Andes.getInstance().messageReceived(andesMessage); if (log.isDebugEnabled()) { log.debug(" Message added with message id " + mqttLocalMessageID); } } else { throw new MQTTException("Message content is not backed by an array, or the array is read-only ."); } }
From source file:org.xenei.bloomgraph.bloom.sql.DBIO.java
/** * Convert the byte buffer to a input stream. * /*from w ww . ja v a 2 s . co m*/ * @param buffer * the buffer to conver * @return the input stream. */ public static InputStream asInputStream(final ByteBuffer buffer) { final ByteBuffer buff = buffer.slice().order(ByteOrder.LITTLE_ENDIAN); if (buff.hasArray()) { // use heap buffer; no array is created; only the reference is used return new ByteArrayInputStream(buff.array()); } return new ByteBufferInputStream(buff); }
From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinRawBlockRecordReader.java
/** * * Read a next block. /*from w w w . jav a 2 s. c om*/ * * @param key is a 64 byte array (hashMerkleRoot and prevHashBlock) * @param value is a deserialized Java object of class BitcoinBlock * * @return true if next block is available, false if not */ public boolean next(BytesWritable key, BytesWritable value) throws IOException { // read all the blocks, if necessary a block overlapping a split while (getFilePosition() <= getEnd()) { // did we already went beyond the split (remote) or do we have no further data left? ByteBuffer dataBlock = null; try { dataBlock = getBbr().readRawBlock(); } catch (BitcoinBlockReadException e) { // log LOG.error(e); } if (dataBlock == null) return false; byte newKey[] = getBbr().getKeyFromRawBlock(dataBlock); key.set(newKey, 0, newKey.length); byte[] dataBlockArray = null; if (dataBlock.hasArray() == true) { dataBlockArray = dataBlock.array(); } else { dataBlockArray = new byte[dataBlock.capacity()]; dataBlock.get(dataBlockArray); } value.set(dataBlockArray, 0, dataBlockArray.length); return true; } return false; }
From source file:tachyon.io.Utils.java
/** * Converts a byte buffer to a base64-encoded String. Avoids copying the array if possible. *///from ww w. ja va2 s .c om public static String byteBufferToBase64(ByteBuffer buf) { if (buf == null) { return null; } if (buf.hasArray() && buf.position() == 0 && buf.limit() == buf.capacity()) { return Base64.encodeBase64String(buf.array()); } else { byte[] b = new byte[buf.remaining()]; buf.get(b); return Base64.encodeBase64String(b); } }
From source file:wicket.protocol.http.FilePageStore.java
/** * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#getPage(java.lang.String, * int, int)/*from w w w . j a va 2 s. c om*/ */ public Page getPage(String sessionId, int id, int versionNumber) { File sessionDir = new File(workDir, sessionId); if (sessionDir.exists()) { File pageFile = getPageFile(id, versionNumber, sessionDir); if (pageFile.exists()) { FileInputStream fis = null; try { byte[] pageData = null; fis = new FileInputStream(pageFile); int length = (int) pageFile.length(); ByteBuffer bb = ByteBuffer.allocate(length); fis.getChannel().read(bb); if (bb.hasArray()) { pageData = bb.array(); } else { pageData = new byte[length]; bb.get(pageData); } Page page = (Page) Objects.byteArrayToObject(pageData); return page.getVersion(versionNumber); } catch (Exception e) { log.debug("Error loading page " + id + " with version " + versionNumber + " for the sessionid " + sessionId + " from disc", e); } finally { try { if (fis != null) { fis.close(); } } catch (IOException ex) { // ignore } } } } return null; }