DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Public Member Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Protocol.LengthMarkedBufferedInputStream Class Reference

Reads a command block on the underlying stream that is constrained by a length marker preceeding the command. More...

Inheritance diagram for Deveel.Data.Protocol.LengthMarkedBufferedInputStream:

Public Member Functions

 LengthMarkedBufferedInputStream (IInputStream input)
 
override int ReadByte ()
 
override void Write (byte[] buffer, int offset, int count)
 
override void Flush ()
 
override long Seek (long offset, SeekOrigin origin)
 
override void SetLength (long value)
 
override int Read (byte[] b, int off, int len)
 
bool PollForCommand (int maxSize)
 Checks to see if there is a complete command waiting on the input stream. More...
 
void BlockForCommand ()
 Blocks until a complete command has been read in. More...
 

Properties

override bool CanRead [get]
 
override bool CanSeek [get]
 
override bool CanWrite [get]
 
override long Length [get]
 
override long Position [get, set]
 
int Available [get]
 

Private Member Functions

void EnsureCapacity (int newSize)
 Ensures that the buffer is large enough to store the given value. More...
 
void HandleEndReached ()
 Called when the end of the marked length is reached. More...
 

Private Attributes

const int InitialBufferSize = 512
 The initial buffer size of the internal input buffer. More...
 
readonly IInputStream input
 The chained InputStream that is underneath this object. More...
 
byte[] buf
 The buffer that is used to read in whatever is on the stream. More...
 
int count
 The number of valid bytes in the buffer. More...
 
int markedLength
 The area of the buffer that is marked as being an available command. If it's -1 then there is no area marked. More...
 
int markedIndex
 The current index of the marked area that is being read. More...
 

Detailed Description

Reads a command block on the underlying stream that is constrained by a length marker preceeding the command.

This can be used as a hack work around for non-blocking IO because we know ahead of time how much data makes up the next block of information over the stream.

Definition at line 16 of file LengthMarkedBufferedInputStream.cs.

Constructor & Destructor Documentation

Deveel.Data.Protocol.LengthMarkedBufferedInputStream.LengthMarkedBufferedInputStream ( IInputStream  input)
inline

Definition at line 48 of file LengthMarkedBufferedInputStream.cs.

48  {
49  this.input = input;
50  buf = new byte[InitialBufferSize];
51  count = 0;
52  markedLength = -1;
53  markedIndex = -1;
54  }
const int InitialBufferSize
The initial buffer size of the internal input buffer.
readonly IInputStream input
The chained InputStream that is underneath this object.
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.

Member Function Documentation

void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.BlockForCommand ( )
inline

Blocks until a complete command has been read in.

Definition at line 247 of file LengthMarkedBufferedInputStream.cs.

247  {
248  lock (this) {
249  while (true) {
250  // Is there a command available?
251  if (count >= 4) {
252  int lengthMarker = ByteBuffer.ReadInt4(buf, 0);
253  if (count >= lengthMarker + 4) {
254  // Yes, complete command available.
255  // mark this area up.
256  markedLength = lengthMarker + 4;
257  markedIndex = 4;
258  return;
259  }
260  }
261 
262  // If the buffer is full grow it larger.
263  if (count >= buf.Length) {
265  }
266  // Read in a block of data, block if nothing there
267  int read_in = input.Read(buf, count, buf.Length - count);
268  if (read_in == 0) {
269  //TODO: Check this format...
270  // throw new EndOfStreamException();
271 
272  // zero bytes read means that the stream is finished...
273  return;
274  }
275  count += read_in;
276  }
277  }
278  }
const int InitialBufferSize
The initial buffer size of the internal input buffer.
int ReadInt4()
Reads an integer from the buffer at the current position.
Definition: ByteBuffer.cs:128
readonly IInputStream input
The chained InputStream that is underneath this object.
void EnsureCapacity(int newSize)
Ensures that the buffer is large enough to store the given value.
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
A wrapper for an array of byte.
Definition: ByteBuffer.cs:27
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.
int Read(byte[] bytes, int offset, int length)
void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.EnsureCapacity ( int  newSize)
inlineprivate

Ensures that the buffer is large enough to store the given value.

Parameters
newSize

If the buffer is not large enough this method grows it so it is big enough.

Definition at line 63 of file LengthMarkedBufferedInputStream.cs.

63  {
64  int old_size = buf.Length;
65  if (newSize > old_size) {
66  int cap = (old_size * 3) / 2 + 1;
67  if (cap < newSize)
68  cap = newSize;
69  byte[] oldBuf = buf;
70  buf = new byte[cap];
71  // // Copy all the contents except the first 4 bytes (the size marker)
72  Array.Copy(oldBuf, 0, buf, 0, count);
73  }
74  }
byte[] buf
The buffer that is used to read in whatever is on the stream.
override void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Flush ( )
inline

Definition at line 143 of file LengthMarkedBufferedInputStream.cs.

143  {
144  }
void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.HandleEndReached ( )
inlineprivate

Called when the end of the marked length is reached.

It performs various maintenance operations to ensure the buffer consistency is maintained.

Assumes we are calling from a synchronized method.

Definition at line 86 of file LengthMarkedBufferedInputStream.cs.

86  {
87  // Move anything from the end of the buffer to the start.
88  Array.Copy(buf, markedIndex, buf, 0, count - markedLength);
90 
91  // Reset the state
92  markedLength = -1;
93  markedIndex = -1;
94  }
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.
bool Deveel.Data.Protocol.LengthMarkedBufferedInputStream.PollForCommand ( int  maxSize)
inline

Checks to see if there is a complete command waiting on the input stream.

Parameters
maxSizeThe maximum number of bytes we are allowing before an IOException is thrown.

If this method returns true then it is safe to go ahead and process a single command from this stream. This will return true only once while there is a command pending until that command is completely read in.

Returns
Returns true if there is a complete command.

Definition at line 204 of file LengthMarkedBufferedInputStream.cs.

204  {
205  lock (this) {
206  if (markedLength == -1) {
207  int available = input.Available;
208  if (count > 0 || available > 0) {
209  if ((count + available) > maxSize) {
210  throw new IOException("Marked length is greater than max size ( " +
211  (count + available) + " > " + maxSize + " )");
212  }
213 
214  EnsureCapacity(count + available);
215  int readIn = input.Read(buf, count, available);
216 
217  if (readIn == 0) {
218  //TODO: Check this format...
219  // throw new EndOfStreamException();
220 
221  // zero bytes read means that the stream is finished...
222  return false;
223  }
224  count = count + readIn;
225 
226  // Check: Is a complete command available?
227  if (count >= 4) {
228  int lengthMarker = ByteBuffer.ReadInt4(buf, 0);
229 
230  if (count >= lengthMarker + 4) {
231  // Yes, complete command available.
232  // mark this area up.
233  markedLength = lengthMarker + 4;
234  markedIndex = 4;
235  return true;
236  }
237  }
238  }
239  }
240  return false;
241  }
242  }
int ReadInt4()
Reads an integer from the buffer at the current position.
Definition: ByteBuffer.cs:128
readonly IInputStream input
The chained InputStream that is underneath this object.
void EnsureCapacity(int newSize)
Ensures that the buffer is large enough to store the given value.
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
int Available
Gets ths available bytes to be read on the underlying stream.
Definition: IInputStream.cs:14
A wrapper for an array of byte.
Definition: ByteBuffer.cs:27
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.
int Read(byte[] bytes, int offset, int length)
override int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Read ( byte[]  b,
int  off,
int  len 
)
inline

Definition at line 154 of file LengthMarkedBufferedInputStream.cs.

154  {
155  lock (this) {
156  if (markedIndex == -1)
157  throw new IOException("No mark has been read yet.");
158 
159  int readUpto = markedIndex + len;
160  if (readUpto > markedLength) {
161  String debug_msg = "Read over end of length marked buffer. ";
162  debug_msg += "(marked_index=" + markedIndex;
163  debug_msg += ",len=" + len;
164  debug_msg += ",marked_length=" + markedLength + ")";
165  throw new IOException(debug_msg);
166  }
167  Array.Copy(buf, markedIndex, b, off, len);
168  markedIndex = readUpto;
169  if (markedIndex >= markedLength) {
171  }
172  return len;
173  }
174  }
A long string in the system.
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.
void HandleEndReached()
Called when the end of the marked length is reached.
override int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.ReadByte ( )
inline

Definition at line 98 of file LengthMarkedBufferedInputStream.cs.

98  {
99  lock (this) {
100  if (markedIndex == -1)
101  throw new IOException("No mark has been read yet.");
102 
103  if (markedIndex >= markedLength) {
104  string debugMsg = "Read over end of length marked buffer. ";
105  debugMsg += "(marked_index=" + markedIndex;
106  debugMsg += ",marked_length=" + markedLength + ")";
107  debugMsg += ")";
108  throw new IOException(debugMsg);
109  }
110  int n = buf[markedIndex++] & 0x0FF;
111  if (markedIndex >= markedLength) {
113  }
114  return n;
115  }
116  }
int markedLength
The area of the buffer that is marked as being an available command. If it's -1 then there is no area...
int markedIndex
The current index of the marked area that is being read.
byte[] buf
The buffer that is used to read in whatever is on the stream.
void HandleEndReached()
Called when the end of the marked length is reached.
override long Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Seek ( long  offset,
SeekOrigin  origin 
)
inline

Definition at line 146 of file LengthMarkedBufferedInputStream.cs.

146  {
147  throw new NotSupportedException();
148  }
override void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.SetLength ( long  value)
inline

Definition at line 150 of file LengthMarkedBufferedInputStream.cs.

150  {
151  throw new NotSupportedException();
152  }
override void Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Write ( byte[]  buffer,
int  offset,
int  count 
)
inline

Definition at line 118 of file LengthMarkedBufferedInputStream.cs.

118  {
119  throw new NotSupportedException();
120  }

Member Data Documentation

byte [] Deveel.Data.Protocol.LengthMarkedBufferedInputStream.buf
private

The buffer that is used to read in whatever is on the stream.

Definition at line 30 of file LengthMarkedBufferedInputStream.cs.

int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.count
private

The number of valid bytes in the buffer.

Definition at line 35 of file LengthMarkedBufferedInputStream.cs.

const int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.InitialBufferSize = 512
private

The initial buffer size of the internal input buffer.

Definition at line 20 of file LengthMarkedBufferedInputStream.cs.

readonly IInputStream Deveel.Data.Protocol.LengthMarkedBufferedInputStream.input
private

The chained InputStream that is underneath this object.

Definition at line 25 of file LengthMarkedBufferedInputStream.cs.

int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.markedIndex
private

The current index of the marked area that is being read.

Definition at line 46 of file LengthMarkedBufferedInputStream.cs.

int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.markedLength
private

The area of the buffer that is marked as being an available command. If it's -1 then there is no area marked.

Definition at line 41 of file LengthMarkedBufferedInputStream.cs.

Property Documentation

int Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Available
get

Definition at line 176 of file LengthMarkedBufferedInputStream.cs.

override bool Deveel.Data.Protocol.LengthMarkedBufferedInputStream.CanRead
get

Definition at line 122 of file LengthMarkedBufferedInputStream.cs.

override bool Deveel.Data.Protocol.LengthMarkedBufferedInputStream.CanSeek
get

Definition at line 126 of file LengthMarkedBufferedInputStream.cs.

override bool Deveel.Data.Protocol.LengthMarkedBufferedInputStream.CanWrite
get

Definition at line 130 of file LengthMarkedBufferedInputStream.cs.

override long Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Length
get

Definition at line 134 of file LengthMarkedBufferedInputStream.cs.

override long Deveel.Data.Protocol.LengthMarkedBufferedInputStream.Position
getset

Definition at line 138 of file LengthMarkedBufferedInputStream.cs.


The documentation for this class was generated from the following file: