/// <summary> /// Create a new Blob from an existing ByteBuffer. IMPORTANT: If copy is /// false, after calling this constructor, if you keep a pointer to the buffer /// then you must treat it as immutable and promise not to change it. /// </summary> /// /// <param name="buffer"></param> /// <param name="copy"></param> public Blob(ByteBuffer buffer, bool copy) { this.haveHashCode_ = false; if (buffer != null) { if (copy) { buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(buffer.remaining()); // Put updates buffer.position(), so save and restore it. int savePosition = buffer.position(); buffer_.put(buffer); buffer.position(savePosition); buffer_.flip(); } else buffer_ = buffer.slice(); } else buffer_ = null; }
/// <summary> /// Reverse the bytes in buffer starting at position, up to but not including /// limit. /// </summary> /// /// <param name="buffer"></param> /// <param name="position"></param> /// <param name="limit"></param> public static void reverse(ByteBuffer buffer, int position, int limit) { int from = position; int to = limit - 1; while (from < to) { // swap byte temp = buffer.get(from); buffer.put(from, buffer.get(to)); buffer.put(to, temp); --to; ++from; } }
// fh is filehandle passed from open //throws FuseException public int read(String path, Object fh, ByteBuffer buf, long offset) { if (fh is FH) { F f = (F)((FH)fh).n; buf.put(f.content, (int)offset, System.Math.Min(buf.remaining(), f.content.Length - (int)offset)); return 0; } return Errno.EBADF; }
// // XattrSupport implementation /** * This method will be called to get the value of the extended attribute * * @params path the path to file or directory containing extended attribute * @params name the name of the extended attribute * @params dst a ByteBuffer that should be filled with the value of the extended attribute * @return 0 if Ok or errno when error * @throws fuse.FuseException an alternative to returning errno is to throw this exception with errno initialized * @throws java.nio.BufferOverflowException * should be thrown to indicate that the given <code>dst</code> ByteBuffer * is not large enough to hold the attribute's value. After that <code>getxattr()</code> method will * be called again with a larger buffer. */ //throws FuseException, BufferOverflowException public int getxattr(String path, String name, ByteBuffer dst) { N n = lookup(path); if (n == null) return Errno.ENOENT; byte[] value = null; n.xattrs.TryGetValue(name, out value); if (value == null) return Errno.ENOATTR; dst.put(value); return 0; }
/** * Write the length of the compressed bytes. Life is much easier if the * header is constant length, so just use 3 bytes. Considering most of the * codecs want between 32k (snappy) and 256k (lzo, zlib), 3 bytes should * be plenty. We also use the low bit for whether it is the original or * compressed bytes. * @param buffer the buffer to write the header to * @param position the position in the buffer to write at * @param val the size in the file * @param original is it uncompressed */ private static void writeHeader( ByteBuffer buffer, int position, int val, bool original) { buffer.put(position, (byte)((val << 1) + (original ? 1 : 0))); buffer.put(position + 1, (byte)(val >> 7)); buffer.put(position + 2, (byte)(val >> 15)); }
/// <summary> /// Create a new Blob with a copy of the bytes in the array. /// </summary> /// /// <param name="value"></param> public Blob(int[] value_ren) { this.haveHashCode_ = false; buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(value_ren.Length); for (int i = 0; i < value_ren.Length; ++i) buffer_.put((byte) value_ren[i]); buffer_.flip(); }
/// <summary> /// Create a new Blob from the UTF-8 encoding of the Unicode string. /// </summary> /// /// <param name="value">The Unicode string which is encoded as UTF-8.</param> public Blob(String value_ren) { this.haveHashCode_ = false; byte[] utf8; try { utf8 = ILOG.J2CsMapping.Util.StringUtil.GetBytes(value_ren,"UTF-8"); } catch (IOException ex) { // We don't expect this to happen. throw new Exception("UTF-8 encoder not supported: " + ex.Message); } buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(utf8.Length); buffer_.put(utf8); buffer_.flip(); }
/// <summary> /// Create a new Blob with a copy of the bytes in the array. /// </summary> /// /// <param name="value">The byte array to copy.</param> public Blob(byte[] value_ren) { this.haveHashCode_ = false; buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(value_ren.Length); buffer_.put(value_ren); buffer_.flip(); }
/// <summary> /// Create a new Blob from the the byte array. IMPORTANT: If copy is false, /// after calling this constructor, if you keep a pointer to the buffer then /// you must treat it as immutable and promise not to change it. /// </summary> /// /// <param name="value">The byte array. If copy is true, this makes a copy.</param> /// <param name="copy"></param> public Blob(byte[] value_ren, bool copy) { this.haveHashCode_ = false; if (copy) { buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(value_ren.Length); buffer_.put(value_ren); buffer_.flip(); } else buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.wrap(value_ren); }
public static void readDirect(Stream file, int len, ByteBuffer directBuf) { // TODO: HDFS API is a mess, so handle all kinds of cases. // Before 2.7, read() also doesn't adjust position correctly, so track it separately. int pos = directBuf.position(), startPos = pos, endPos = pos + len; try { while (pos < endPos) { int count = readByteBuffer(file, directBuf); if (count < 0) throw new EndOfStreamException(); Debug.Assert(count != 0, "0-length read: " + (endPos - pos) + "@" + (pos - startPos)); pos += count; Debug.Assert(pos <= endPos, "Position " + pos + " > " + endPos + " after reading " + count); directBuf.position(pos); } } catch (NotSupportedException) { Debug.Assert(pos == startPos); // Happens in q files and such. RecordReaderImpl.LOG.error("Stream does not support direct read; we will copy."); byte[] buffer = new byte[len]; file.readFully(buffer, 0, buffer.Length); directBuf.put(buffer); } directBuf.position(startPos); directBuf.limit(startPos + len); }
public void setByteBuffer(ByteBuffer result, int offset, int length) { result.clear(); int currentChunk = offset / chunkSize; int currentOffset = offset % chunkSize; while (length > 0) { int currentLength = Math.Min(length, chunkSize - currentOffset); result.put(data[currentChunk], currentOffset, currentLength); length -= currentLength; currentChunk++; currentOffset = 0; } }
/** * Send an object through the telemetry link. * @throws IOException * @param[in] obj Object handle to send * @param[in] type Transaction type \return Success (true), Failure (false) */ private bool transmitSingleObject(UAVObject obj, int type, bool allInstances) { int length; int allInstId = uavConsts.ALL_INSTANCES; ByteBuffer bbuf = new ByteBuffer(uavConsts.MAX_PACKET_LENGTH); // Determine data length if (type == uavConsts.TYPE_OBJ_REQ || type == uavConsts.TYPE_ACK) { length = 0; } else { length = obj.getNumBytes(); } // Setup type and object id fields bbuf.put((byte)(uavConsts.SYNC_VAL & 0xff)); bbuf.put((byte)(type & 0xff)); bbuf.putShort((UInt16)(length + 2 /* SYNC, Type */+ 2 /* Size */+ 4 /* ObjID */+ (obj .isSingleInstance() ? 0 : 2))); bbuf.putUint32((UInt32)obj.getObjID()); // Setup instance ID if one is required if (!obj.isSingleInstance()) { // Check if all instances are requested if (allInstances) bbuf.putShort((UInt16)(allInstId & 0xffff)); else bbuf.putShort((UInt16)(obj.getInstID() & 0xffff)); } // Check length if (length >= uavConsts.MAX_PAYLOAD_LENGTH) return false; // Copy data (if any) if (length > 0) try { if (obj.pack(bbuf) == 0) return false; } catch (Exception e) { // TODO Auto-generated catch block Debug.Write(e.Message); return false; } // Calculate checksum bbuf.put((byte)(CRC.updateCRC(0, bbuf.array(), bbuf.position()) & 0xff)); int packlen = bbuf.position(); bbuf.position(0); byte[] dst = new byte[packlen]; bbuf.get(dst, 0, packlen); if (type == uavConsts.TYPE_OBJ_ACK || type == uavConsts.TYPE_OBJ_REQ) { // Once we send a UAVTalk packet that requires an ack or object let's set up // the transaction here setupTransaction(obj, allInstances, type); } ch.write(dst); // Update stats ++txStats.Objects; txStats.Bytes += bbuf.position(); txStats.ObjectBytes += length; // Done return true; }