DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlBinary.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 using System.Collections;
19 using System.Collections.Generic;
20 using System.IO;
21 
22 namespace Deveel.Data.Sql.Objects {
27  public struct SqlBinary : ISqlBinary {
28  private readonly byte[] bytes;
29  public static readonly SqlBinary Null = new SqlBinary(null);
30 
36  public const int MaxLength = Int32.MaxValue - 1;
37 
38  public SqlBinary(byte[] source)
39  : this(source, source == null ? 0 : source.Length) {
40  }
41 
42  public SqlBinary(byte[] source, int length)
43  : this(source, 0, length) {
44  }
45 
46  public SqlBinary(byte[] source, int offset, int length)
47  : this() {
48  if (source == null) {
49  bytes = null;
50  } else {
51  if (length > MaxLength)
52  throw new ArgumentOutOfRangeException("length");
53 
54  bytes = new byte[length];
55  Array.Copy(source, offset, bytes, 0, length);
56  }
57  }
58 
59  int IComparable.CompareTo(object obj) {
60  return (this as IComparable<ISqlObject>).CompareTo((ISqlObject) obj);
61  }
62 
63  int IComparable<ISqlObject>.CompareTo(ISqlObject other) {
64  throw new NotSupportedException();
65  }
66 
68  public bool IsNull {
69  get { return bytes == null; }
70  }
71 
73  return false;
74  }
75 
77  public IEnumerator<byte> GetEnumerator() {
78  throw new NotImplementedException();
79  }
80 
81  IEnumerator IEnumerable.GetEnumerator() {
82  return GetEnumerator();
83  }
84 
86  public long Length {
87  get { return bytes == null ? 0L : bytes.Length; }
88  }
89 
91  public Stream GetInput() {
92  if (IsNull)
93  return Stream.Null;
94 
95  return new MemoryStream(bytes, false);
96  }
97 
102  public byte[] ToByteArray() {
103  if (bytes == null)
104  return new byte[0];
105 
106  var destArray = new byte[bytes.Length];
107  Array.Copy(bytes, 0, destArray, 0, bytes.Length);
108  return destArray;
109  }
110  }
111 }
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
Definition: SqlBinary.cs:27
Stream GetInput()
Gets an object used to read the contents of the binary
Definition: SqlBinary.cs:91
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
byte[] ToByteArray()
Returns an array of bytes representing the contents of the binary.
Definition: SqlBinary.cs:102
SqlBinary(byte[] source, int length)
Definition: SqlBinary.cs:42
SqlBinary(byte[] source, int offset, int length)
Definition: SqlBinary.cs:46
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
Defines the required contract of a SQL BINARY object
Definition: ISqlBinary.cs:25
IEnumerator< byte > GetEnumerator()
Definition: SqlBinary.cs:77