本文整理汇总了C#中System.ByteBuffer.remaining方法的典型用法代码示例。如果您正苦于以下问题:C# ByteBuffer.remaining方法的具体用法?C# ByteBuffer.remaining怎么用?C# ByteBuffer.remaining使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.remaining方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Blob
/// <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;
}
示例2: onReceivedData
/// <summary>
/// Continue to read data until the end of an element, then call
/// elementListener.onReceivedElement(element ). The buffer passed to
/// onReceivedElement is only valid during this call. If you need the data
/// later, you must copy.
/// </summary>
///
/// <param name="data"></param>
/// <exception cref="EncodingException">For invalid encoding.</exception>
public void onReceivedData(ByteBuffer data)
{
// We may repeatedly set data to a slice as we read elements.
data = data.slice();
// Process multiple objects in the data.
while (true) {
bool gotElementEnd;
int offset;
try {
if (!usePartialData_) {
// This is the beginning of an element.
if (data.remaining() <= 0)
// Wait for more data.
return;
}
// Scan the input to check if a whole TLV object has been read.
tlvStructureDecoder_.seek(0);
gotElementEnd = tlvStructureDecoder_.findElementEnd(data);
offset = tlvStructureDecoder_.getOffset();
} catch (EncodingException ex) {
// Reset to read a new element on the next call.
usePartialData_ = false;
tlvStructureDecoder_ = new TlvStructureDecoder();
throw ex;
}
if (gotElementEnd) {
// Got the remainder of an element. Report to the caller.
ByteBuffer element;
if (usePartialData_) {
// We have partial data from a previous call, so append this data and point to partialData.
partialData_.ensuredPut(data, 0, offset);
element = partialData_.flippedBuffer();
// Assume we don't need to use partialData anymore until needed.
usePartialData_ = false;
} else {
// We are not using partialData, so just point to the input data buffer.
element = data.duplicate();
element.limit(offset);
}
// Reset to read a new object. Do this before calling onReceivedElement
// in case it throws an exception.
data.position(offset);
data = data.slice();
tlvStructureDecoder_ = new TlvStructureDecoder();
elementListener_.onReceivedElement(element);
if (data.remaining() <= 0)
// No more data in the packet.
return;
// else loop back to decode.
} else {
// Save remaining data for a later call.
if (!usePartialData_) {
usePartialData_ = true;
partialData_.position(0);
}
if (partialData_.buffer().position() + data.remaining() > net.named_data.jndn.util.Common.MAX_NDN_PACKET_SIZE) {
// Reset to read a new element on the next call.
usePartialData_ = false;
tlvStructureDecoder_ = new TlvStructureDecoder();
throw new EncodingException(
"The incoming packet exceeds the maximum limit Face.getMaxNdnPacketSize()");
}
partialData_.ensuredPut(data);
return;
}
}
}
示例3: slice
/* slices a read only contiguous buffer of chunkLength */
private ByteBuffer slice(int chunkLength)
{
int len = chunkLength;
long oldOffset = currentOffset;
ByteBuffer slice;
if (compressed.remaining() >= len)
{
slice = compressed.slice();
// simple case
slice.limit(len);
currentOffset += len;
compressed.position(compressed.position() + len);
return slice;
}
else if (currentRange >= (bytes.Count - 1))
{
// nothing has been modified yet
throw new IOException("EOF in " + this + " while trying to read " +
chunkLength + " bytes");
}
if (LOG.isDebugEnabled())
{
LOG.debug(String.Format(
"Crossing into next BufferChunk because compressed only has %d bytes (needs %d)",
compressed.remaining(), len));
}
// we need to consolidate 2 or more buffers into 1
// first copy out compressed buffers
ByteBuffer copy = allocateBuffer(chunkLength, compressed.isDirect());
currentOffset += compressed.remaining();
len -= compressed.remaining();
copy.put(compressed);
for (int i = currentRange; i < bytes.Count && len > 0; i++)
{
++currentRange;
if (LOG.isDebugEnabled())
{
LOG.debug(String.Format("Read slow-path, >1 cross block reads with {0}", this.ToString()));
}
DiskRange range = bytes[i];
compressed = range.getData().duplicate();
if (compressed.remaining() >= len)
{
slice = compressed.slice();
slice.limit(len);
copy.put(slice);
currentOffset += len;
compressed.position(compressed.position() + len);
return copy;
}
currentOffset += compressed.remaining();
len -= compressed.remaining();
copy.put(compressed);
}
// restore offsets for exception clarity
seek(oldOffset);
throw new IOException("EOF in " + this + " while trying to read " +
chunkLength + " bytes");
}
示例4: writeBlobTlv
/// <summary>
/// Write the type, then the length of the buffer then the buffer value from
/// its position() to limit() to the output just before getLength() from the
/// back. Advance getLength() of the output. This does NOT change
/// value.position().
/// </summary>
///
/// <param name="type"></param>
/// <param name="value"></param>
public void writeBlobTlv(int type, ByteBuffer value_ren)
{
if (value_ren == null) {
writeTypeAndLength(type, 0);
return;
}
// Write backwards.
writeBuffer(value_ren);
writeTypeAndLength(type, value_ren.remaining());
}
示例5: writeOptionalBlobTlv
/// <summary>
/// If the byte buffer value is null or value.remaining() is zero then do
/// nothing, otherwise call writeBlobTlv.
/// </summary>
///
/// <param name="type"></param>
/// <param name="value"></param>
public void writeOptionalBlobTlv(int type, ByteBuffer value_ren)
{
if (value_ren != null && value_ren.remaining() > 0)
writeBlobTlv(type, value_ren);
}
示例6: ensuredPut
/// <summary>
/// Call ensureCapacity to ensure there is capacity for buffer.remaining() more
/// bytes and use buffer().put to copy.
/// This increments the position by buffer.remaining().
/// This does update buffer's position to its limit.
/// </summary>
///
/// <param name="buffer"></param>
public void ensuredPut(ByteBuffer buffer)
{
ensureRemainingCapacity(buffer.remaining());
int savePosition = buffer.position();
buffer_.put(buffer);
buffer.position(savePosition);
}
示例7: toEscapedString
/// <summary>
/// Convert the value by escaping characters according to the NDN URI Scheme.
/// This also adds "..." to a value with zero or more ".".
/// This does not add a type code prefix such as "sha256digest=".
/// </summary>
///
/// <param name="value"></param>
/// <returns>The escaped string.</returns>
public static String toEscapedString(ByteBuffer value_ren)
{
StringBuilder result = new StringBuilder(value_ren.remaining());
toEscapedString(value_ren, result);
return result.toString();
}
示例8: DerByteString
/// <summary>
/// Create a DerByteString with the given inputData and nodeType. This is a
/// private constructor used by one of the public subclasses such as
/// DerOctetString or DerPrintableString.
/// </summary>
///
/// <param name="inputData"></param>
/// <param name="nodeType">The specific DER node type, a value from DerNodeType.</param>
public DerByteString(ByteBuffer inputData, int nodeType)
: base(nodeType)
{
if (inputData != null) {
payload_.ensuredPut(inputData);
encodeHeader(inputData.remaining());
}
}
示例9: toHex
/// <summary>
/// Return a hex string of the contents of buffer from position to limit.
/// </summary>
///
/// <param name="buffer">The buffer.</param>
/// <returns>A string of hex bytes.</returns>
public static String toHex(ByteBuffer buffer)
{
StringBuilder output = new StringBuilder(buffer.remaining() * 2);
toHex(buffer, output);
return output.toString();
}
示例10: decodeToBitmap
private Bitmap decodeToBitmap(ByteBuffer jpegData, int sampleSize)
{
sbyte[] jpegDataArray = new sbyte[jpegData.remaining()];
jpegData.get(jpegDataArray);
jpegData.rewind();
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = sampleSize;
return BitmapFactory.decodeByteArray(jpegDataArray, 0, jpegDataArray.Length, option);
}
示例11: send
/// <summary>
/// Send the encoded packet out through the transport.
/// </summary>
///
/// <param name="encoding"></param>
/// <exception cref="System.Exception">If the encoded packet size exceeds getMaxNdnPacketSize().</exception>
public void send(ByteBuffer encoding)
{
if (encoding.remaining() > getMaxNdnPacketSize())
throw new Exception(
"The encoded packet size exceeds the maximum limit getMaxNdnPacketSize()");
transport_.send(encoding);
}
示例12: onReceivedElement
public void onReceivedElement(ByteBuffer element)
{
LpPacket lpPacket = null;
if (element.get(0) == net.named_data.jndn.encoding.tlv.Tlv.LpPacket_LpPacket) {
// Decode the LpPacket and replace element with the fragment.
lpPacket = new LpPacket();
// Set copy false so that the fragment is a slice which will be copied below.
// The header fields are all integers and don't need to be copied.
net.named_data.jndn.encoding.TlvWireFormat.get().decodeLpPacket(lpPacket, element, false);
element = lpPacket.getFragmentWireEncoding().buf();
}
// First, decode as Interest or Data.
Interest interest = null;
Data data = null;
if (element.get(0) == net.named_data.jndn.encoding.tlv.Tlv.Interest || element.get(0) == net.named_data.jndn.encoding.tlv.Tlv.Data) {
TlvDecoder decoder = new TlvDecoder(element);
if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Interest, element.remaining())) {
interest = new Interest();
interest.wireDecode(element, net.named_data.jndn.encoding.TlvWireFormat.get());
if (lpPacket != null)
interest.setLpPacket(lpPacket);
} else if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Data, element.remaining())) {
data = new Data();
data.wireDecode(element, net.named_data.jndn.encoding.TlvWireFormat.get());
if (lpPacket != null)
data.setLpPacket(lpPacket);
}
}
if (lpPacket != null) {
// We have decoded the fragment, so remove the wire encoding to save memory.
lpPacket.setFragmentWireEncoding(new Blob());
NetworkNack networkNack = net.named_data.jndn.NetworkNack.getFirstHeader(lpPacket);
if (networkNack != null) {
if (interest == null)
// We got a Nack but not for an Interest, so drop the packet.
return;
ArrayList<PendingInterestTable.Entry> pitEntries = new ArrayList<PendingInterestTable.Entry>();
pendingInterestTable_.extractEntriesForNackInterest(interest,
pitEntries);
for (int i = 0; i < pitEntries.Count; ++i) {
PendingInterestTable.Entry pendingInterest = pitEntries[i];
try {
pendingInterest.getOnNetworkNack().onNetworkNack(
pendingInterest.getInterest(), networkNack);
} catch (Exception ex) {
logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onNack", ex);
}
}
// We have processed the network Nack packet.
return;
}
}
// Now process as Interest or Data.
if (interest != null) {
// Quickly lock and get all interest filter callbacks which match.
ArrayList matchedFilters = new ArrayList();
interestFilterTable_.getMatchedFilters(interest, matchedFilters);
// The lock on interestFilterTable_ is released, so call the callbacks.
for (int i_0 = 0; i_0 < matchedFilters.Count; ++i_0) {
InterestFilterTable.Entry entry = (InterestFilterTable.Entry) matchedFilters[i_0];
try {
entry.getOnInterest().onInterest(
entry.getFilter().getPrefix(), interest,
entry.getFace(), entry.getInterestFilterId(),
entry.getFilter());
} catch (Exception ex_1) {
logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onInterest", ex_1);
}
}
} else if (data != null) {
ArrayList<PendingInterestTable.Entry> pitEntries_2 = new ArrayList<PendingInterestTable.Entry>();
pendingInterestTable_.extractEntriesForExpressedInterest(data,
pitEntries_2);
for (int i_3 = 0; i_3 < pitEntries_2.Count; ++i_3) {
PendingInterestTable.Entry pendingInterest_4 = pitEntries_2[i_3];
try {
pendingInterest_4.getOnData().onData(
pendingInterest_4.getInterest(), data);
} catch (Exception ex_5) {
logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onData", ex_5);
}
}
}
}
示例13: computeHmacWithSha256
/// <summary>
/// Compute the HMAC with SHA-256 of data, as defined in
/// http://tools.ietf.org/html/rfc2104#section-2 .
/// </summary>
///
/// <param name="key">The key byte array.</param>
/// <param name="data">The input byte buffer. This does not change the position.</param>
/// <returns>The HMAC result.</returns>
public static byte[] computeHmacWithSha256(byte[] key, ByteBuffer data)
{
using (var hmac = new HMACSHA256(key)) {
// Copy the buffer to an array.
var array = new byte[data.remaining()];
int savePosition = data.position();
data.get(array);
data.position(savePosition);
return hmac.ComputeHash(array);
}
}
示例14: digestSha256
/// <summary>
/// Compute the sha-256 digest of data.
/// </summary>
///
/// <param name="data">The input byte buffer. This does not change the position.</param>
/// <returns>The digest.</returns>
public static byte[] digestSha256(ByteBuffer data)
{
// Copy the buffer to an array.
var array = new byte[data.remaining()];
int savePosition = data.position();
data.get(array);
data.position(savePosition);
return sha256_.ComputeHash(array);
}
示例15: output
public void output(ByteBuffer buffer)
{
_output.Write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
}