/* Copyright 2010-2014 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
namespace MongoDB.Bson.IO
{
///
/// Represents a byte buffer (backed by various means depending on the implementation).
///
public interface IByteBuffer : IDisposable
{
// properties
///
/// Gets or sets the capacity.
///
///
/// The capacity.
///
int Capacity { get; set; }
///
/// Gets a value indicating whether this instance is read only.
///
///
/// true if this instance is read only; otherwise, false.
///
bool IsReadOnly { get; }
///
/// Gets or sets the length.
///
///
/// The length.
///
int Length { get; set; }
///
/// Gets or sets the position.
///
///
/// The position.
///
int Position { get; set; }
// methods
///
/// Clears this instance.
///
void Clear();
///
/// Finds the next null byte.
///
/// The position of the next null byte.
int FindNullByte();
///
/// Gets a slice of this buffer.
///
/// The position of the start of the slice.
/// The length of the slice.
/// A slice of this buffer.
IByteBuffer GetSlice(int position, int length);
///
/// Loads the buffer from a stream.
///
/// The stream.
/// The count.
void LoadFrom(Stream stream, int count);
///
/// Makes this buffer read only.
///
void MakeReadOnly();
///
/// Read directly from the backing bytes. The returned ArraySegment points directly to the backing bytes for
/// the current position and you can read the bytes directly from there. If the backing bytes happen to span
/// a chunk boundary shortly after the current position there might not be enough bytes left in the current
/// chunk in which case the returned ArraySegment will have a Count of zero and you should call ReadBytes instead.
///
/// When ReadBackingBytes returns the position will have been advanced by count bytes *if and only if* there
/// were count bytes left in the current chunk.
///
/// The number of bytes you need to read.
/// An ArraySegment pointing directly to the backing bytes for the current position.
ArraySegment ReadBackingBytes(int count);
///
/// Reads a byte.
///
/// A byte.
byte ReadByte();
///
/// Reads bytes.
///
/// The destination.
/// The destination offset.
/// The count.
void ReadBytes(byte[] destination, int destinationOffset, int count);
///
/// Reads bytes.
///
/// The count.
/// The bytes.
byte[] ReadBytes(int count);
///
/// Write directly to the backing bytes. The returned ArraySegment points directly to the backing bytes for
/// the current position and you can write the bytes directly to there. If the backing bytes happen to span
/// a chunk boundary shortly after the current position there might not be enough bytes left in the current
/// chunk in which case the returned ArraySegment will have a Count of zero and you should call WriteBytes instead.
///
/// When WriteBackingBytes returns the position has not been advanced. After you have written up to count
/// bytes directly to the backing bytes advance the position by the number of bytes actually written.
///
/// The count.
/// An ArraySegment pointing directly to the backing bytes for the current position.
ArraySegment WriteBackingBytes(int count);
///
/// Writes a byte.
///
/// The byte.
void WriteByte(byte source);
///
/// Writes bytes.
///
/// The bytes (in the form of a byte array).
void WriteBytes(byte[] source);
///
/// Writes bytes.
///
/// The bytes (in the form of an IByteBuffer).
void WriteBytes(IByteBuffer source);
///
/// Writes Length bytes from this buffer starting at Position 0 to a stream.
///
/// The stream.
void WriteTo(Stream stream);
}
}