DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ByteBuffer.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 namespace Deveel.Data.Util {
27  class ByteBuffer {
31  private readonly byte[] buf;
32 
36  private int pos;
37 
41  private readonly int length;
42 
49  public ByteBuffer(byte[] buf, int offset, int length) {
50  this.buf = buf;
51  this.length = length;
52  pos = offset;
53  }
54 
55  public ByteBuffer(byte[] buf)
56  : this(buf, 0, buf.Length) {
57  }
58 
62  public int Position {
63  set { pos = value; }
64  get { return pos; }
65  }
66 
70  public int Length {
71  get { return length; }
72  }
73 
81  public ByteBuffer Write(byte[] b, int offset, int count) {
82  Array.Copy(b, offset, buf, pos, count);
83  pos += count;
84  return this;
85  }
86 
87  public ByteBuffer Write(byte[] b) {
88  return Write(b, 0, b.Length);
89  }
90 
96  public ByteBuffer Write(ByteBuffer buffer) {
97  return Write(buffer.buf, buffer.pos, buffer.length);
98  }
99 
107  public ByteBuffer Read(byte[] b, int offset, int count) {
108  Array.Copy(buf, pos, b, offset, count);
109  pos += count;
110  return this;
111  }
112 
118  public ByteBuffer WriteInteger(int v) {
119  WriteInteger(v, buf, pos);
120  pos += 4;
121  return this;
122  }
123 
128  public int ReadInt4() {
129  int v = ReadInt4(buf, pos);
130  pos += 4;
131  return v;
132  }
133 
139  public ByteBuffer WriteByte(byte v) {
140  buf[pos] = v;
141  ++pos;
142  return this;
143  }
144 
149  public byte ReadByte() {
150  byte b = buf[pos];
151  ++pos;
152  return b;
153  }
154 
160  public ByteBuffer WriteInt2(short v) {
161  WriteInt2(v, buf, pos);
162  pos += 2;
163  return this;
164  }
165 
170  public short ReadInt2() {
171  short v = ReadInt2(buf, pos);
172  pos += 2;
173  return v;
174  }
175 
176  public static void WriteInteger(int value, byte[] arr, int offset) {
177  /*
178  TODO: check ...
179  arr[offset + 0] = (byte) ((value >>> 24) & 0xFF);
180  arr[offset + 1] = (byte) ((value >>> 16) & 0xFF);
181  arr[offset + 2] = (byte) ((value >>> 8) & 0xFF);
182  arr[offset + 3] = (byte) ((value >>> 0) & 0xFF);
183  */
184  byte[] buff = BitConverter.GetBytes(value);
185  Array.Copy(buff, 0, arr, offset, buff.Length);
186  }
187 
188  public static char ReadChar(byte[] arr, int offset) {
189  int c1 = (((int) arr[offset + 0]) & 0x0FF);
190  int c2 = (((int) arr[offset + 1]) & 0x0FF);
191  return (char) ((c1 << 8) + (c2));
192  //TODO: check... return BitConverter.ToChar(arr, offset);
193  }
194 
195  public static void WriteChar(char value, byte[] arr, int offset) {
196  arr[offset + 0] = (byte) (URShift(value, 8) & 0x0FF);
197  arr[offset + 1] = (byte) (URShift(value, 0) & 0x0FF);
198  // byte[] buff = BitConverter.GetBytes(value);
199  Array.Copy(arr, 0, arr, offset, 2);
200  }
201 
202  public static short ReadInt2(byte[] arr, int offset) {
203  /*
204  TODO: check ...
205  int c1 = (((int) arr[offset + 0]) & 0x0FF);
206  int c2 = (((int) arr[offset + 1]) & 0x0FF);
207  return (short) ((c1 << 8) + (c2));
208  */
209  return BitConverter.ToInt16(arr, offset);
210  }
211 
212  public static void WriteInt2(short value, byte[] arr, int offset) {
213  /*
214  TODO: check ...
215  arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF);
216  arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF);
217  */
218  byte[] buff = BitConverter.GetBytes(value);
219  Array.Copy(buff, 0, arr, offset, buff.Length);
220  }
221 
222  public static int ReadInt4(byte[] arr, int offset) {
223  /*
224  TODO: check ...
225  int c1 = (((int) arr[offset + 0]) & 0x0FF);
226  int c2 = (((int) arr[offset + 1]) & 0x0FF);
227  int c3 = (((int) arr[offset + 2]) & 0x0FF);
228  int c4 = (((int) arr[offset + 3]) & 0x0FF);
229  return (c1 << 24) + (c2 << 16) + (c3 << 8) + (c4);
230  */
231  return BitConverter.ToInt32(arr, offset);
232  }
233 
234  public static long ReadInt8(byte[] arr, int offset) {
235  /*
236  TODO: check ...
237  long c1 = (((int) arr[offset + 0]) & 0x0FF);
238  long c2 = (((int) arr[offset + 1]) & 0x0FF);
239  long c3 = (((int) arr[offset + 2]) & 0x0FF);
240  long c4 = (((int) arr[offset + 3]) & 0x0FF);
241  long c5 = (((int) arr[offset + 4]) & 0x0FF);
242  long c6 = (((int) arr[offset + 5]) & 0x0FF);
243  long c7 = (((int) arr[offset + 6]) & 0x0FF);
244  long c8 = (((int) arr[offset + 7]) & 0x0FF);
245 
246  return (c1 << 56) + (c2 << 48) + (c3 << 40) +
247  (c4 << 32) + (c5 << 24) + (c6 << 16) + (c7 << 8) + (c8);
248  */
249  return BitConverter.ToInt64(arr, offset);
250  }
251 
252  public static void WriteInt8(long value, byte[] arr, int offset) {
253  /*
254  TODO:
255  arr[offset + 0] = (byte) ((value >>> 56) & 0xFF);
256  arr[offset + 1] = (byte) ((value >>> 48) & 0xFF);
257  arr[offset + 2] = (byte) ((value >>> 40) & 0xFF);
258  arr[offset + 3] = (byte) ((value >>> 32) & 0xFF);
259  arr[offset + 4] = (byte) ((value >>> 24) & 0xFF);
260  arr[offset + 5] = (byte) ((value >>> 16) & 0xFF);
261  arr[offset + 6] = (byte) ((value >>> 8) & 0xFF);
262  arr[offset + 7] = (byte) ((value >>> 0) & 0xFF);
263  */
264  byte[] buff = BitConverter.GetBytes(value);
265  Array.Copy(buff, 0, arr, offset, buff.Length);
266  }
267 
277  public static int URShift(int number, int bits) {
278  if (number >= 0)
279  return number >> bits;
280  return (number >> bits) + (2 << ~bits);
281  }
282 
283  public static int URShift(int number, long bits) {
284  return URShift(number, (int)bits);
285  }
286 
287  public static long URShift(long number, int bits) {
288  if (number >= 0)
289  return number >> bits;
290  return (number >> bits) + (2L << ~bits);
291  }
292 
293  public static long URShift(long number, long bits) {
294  return URShift(number, (int)bits);
295  }
296  }
297 }
ByteBuffer WriteInt2(short v)
Writes a short into the buffer at the current position.
Definition: ByteBuffer.cs:160
short ReadInt2()
Reads a short from the buffer at the current position.
Definition: ByteBuffer.cs:170
byte ReadByte()
Reads a byte from the buffer at the current position.
Definition: ByteBuffer.cs:149
ByteBuffer WriteInteger(int v)
Writes an integer into the buffer at the current position.
Definition: ByteBuffer.cs:118
readonly int length
The length of the buf array.
Definition: ByteBuffer.cs:41
static short ReadInt2(byte[] arr, int offset)
Definition: ByteBuffer.cs:202
int ReadInt4()
Reads an integer from the buffer at the current position.
Definition: ByteBuffer.cs:128
static long URShift(long number, long bits)
Definition: ByteBuffer.cs:293
static void WriteInt2(short value, byte[] arr, int offset)
Definition: ByteBuffer.cs:212
static long URShift(long number, int bits)
Definition: ByteBuffer.cs:287
ByteBuffer(byte[] buf, int offset, int length)
Constructs the buffer.
Definition: ByteBuffer.cs:49
ByteBuffer Write(ByteBuffer buffer)
Writes a ByteBuffer in to this buffer.
Definition: ByteBuffer.cs:96
static int URShift(int number, long bits)
Definition: ByteBuffer.cs:283
static void WriteChar(char value, byte[] arr, int offset)
Definition: ByteBuffer.cs:195
ByteBuffer Write(byte[] b)
Definition: ByteBuffer.cs:87
ByteBuffer Write(byte[] b, int offset, int count)
Writes a byte array into the buffer.
Definition: ByteBuffer.cs:81
static char ReadChar(byte[] arr, int offset)
Definition: ByteBuffer.cs:188
static int URShift(int number, int bits)
Operates a shift on the given integer by the number of bits specified.
Definition: ByteBuffer.cs:277
static int ReadInt4(byte[] arr, int offset)
Definition: ByteBuffer.cs:222
A wrapper for an array of byte.
Definition: ByteBuffer.cs:27
static void WriteInteger(int value, byte[] arr, int offset)
Definition: ByteBuffer.cs:176
readonly byte[] buf
The wrapped byte array itself.
Definition: ByteBuffer.cs:31
ByteBuffer Read(byte[] b, int offset, int count)
Reads a byte array from the buffer.
Definition: ByteBuffer.cs:107
ByteBuffer WriteByte(byte v)
Writes a byte into the buffer at the current position.
Definition: ByteBuffer.cs:139
static long ReadInt8(byte[] arr, int offset)
Definition: ByteBuffer.cs:234
int pos
The current position in the array.
Definition: ByteBuffer.cs:36
static void WriteInt8(long value, byte[] arr, int offset)
Definition: ByteBuffer.cs:252