4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
10 // Author: Tim Coleman <tim@timcoleman.com>
12 // Copyright (C) Tim Coleman, 2003
14 // Licensed under the MIT/X11 License.
18 using System.Data.OracleClient.Oci;
19 using System.Data.SqlTypes;
23 namespace System.Data.OracleClient {
24 public sealed class OracleLob : Stream, ICloneable, IDisposable, INullable
28 public static readonly new OracleLob Null = new OracleLob ();
30 internal OracleConnection connection;
31 bool isBatched = false;
34 OciLobLocator locator;
48 internal OracleLob (OciLobLocator locator, OciDataType ociType)
51 this.locator = locator;
54 case OciDataType.Blob:
55 type = OracleType.Blob;
57 case OciDataType.Clob:
58 type = OracleType.Clob;
63 #endregion // Constructors
67 public override bool CanRead {
68 get { return (IsNull || isOpen); }
71 public override bool CanSeek {
72 get { return (IsNull || isOpen); }
75 public override bool CanWrite {
76 get { return isOpen; }
79 public int ChunkSize {
82 AssertConnectionIsOpen ();
83 AssertObjectNotDisposed ();
84 return locator.GetChunkSize ();
88 public OracleConnection Connection {
89 get { return connection; }
92 public bool IsBatched {
93 get { return isBatched; }
97 get { return !notNull; }
100 public bool IsTemporary {
102 AssertConnectionIsOpen ();
103 AssertObjectNotDisposed ();
104 throw new NotImplementedException ();
108 public override long Length {
110 AssertConnectionIsOpen ();
111 AssertObjectNotDisposed ();
114 return locator.GetLength (LobType == OracleType.Blob);
118 public OracleType LobType {
122 internal OciLobLocator Locator {
123 get { return locator; }
126 public override long Position {
128 AssertConnectionIsOpen ();
129 AssertObjectNotDisposed ();
133 AssertConnectionIsOpen ();
134 AssertObjectNotDisposed ();
139 public object Value {
141 AssertObjectNotDisposed ();
145 byte[] buffer = null;
147 int len = (int) Length;
149 // LOB is not Null, but it is Empty
150 if (LobType == OracleType.Clob)
152 else // OracleType.Blob
156 if (LobType == OracleType.Clob) {
157 buffer = new byte [len];
158 Read (buffer, 0, len);
159 UnicodeEncoding encoding = new UnicodeEncoding ();
160 return encoding.GetString (buffer);
164 buffer = new byte [len];
165 Read (buffer, 0, len);
171 #endregion // Properties
176 public void Append (OracleLob source)
179 throw new ArgumentNullException ();
180 if (Connection.State == ConnectionState.Closed)
181 throw new InvalidOperationException ();
182 throw new NotImplementedException ();
185 void AssertAmountIsEven (long amount, string argName)
188 throw new ArgumentOutOfRangeException ("CLOB and NCLOB parameters require even number of bytes for this argument.");
191 void AssertAmountIsValid (long amount, string argName)
193 if (amount > UInt32.MaxValue)
194 throw new ArgumentOutOfRangeException ("Argument too big.");
195 if (LobType == OracleType.Clob || LobType == OracleType.NClob)
196 AssertAmountIsEven (amount, argName);
199 void AssertConnectionIsOpen ()
201 if (connection.State == ConnectionState.Closed)
202 throw new InvalidOperationException ("Invalid operation. The connection is closed.");
205 void AssertObjectNotDisposed ()
208 throw new ObjectDisposedException ("OracleLob");
211 void AssertTransactionExists ()
213 if (connection.Transaction == null)
214 throw new InvalidOperationException ("Modifying a LOB requires that the connection be transacted.");
217 public void BeginBatch ()
219 BeginBatch (OracleLobOpenMode.ReadOnly);
222 public void BeginBatch (OracleLobOpenMode mode)
224 AssertConnectionIsOpen ();
225 AssertObjectNotDisposed ();
227 locator.BeginBatch (mode);
232 public object Clone ()
234 throw new NotImplementedException ();
238 public override void Close ()
244 public long CopyTo (OracleLob destination)
246 return CopyTo (0, destination, 0, Length);
249 public long CopyTo (OracleLob destination, long destinationOffset)
251 return CopyTo (0, destination, destinationOffset, Length);
254 public long CopyTo (long sourceOffset, OracleLob destination, long destinationOffset, long amount)
256 if (destination.IsNull)
257 throw new ArgumentNullException ();
259 AssertAmountIsValid (sourceOffset, "sourceOffset");
260 AssertAmountIsValid (destinationOffset, "destinationOffset");
261 AssertAmountIsValid (amount, "amount");
262 AssertTransactionExists ();
263 AssertConnectionIsOpen ();
265 return (long) locator.Copy (destination.Locator, (uint) amount, (uint) destinationOffset + 1, (uint) sourceOffset + 1);
269 public void Dispose ()
271 throw new NotImplementedException ();
274 public void EndBatch ()
276 AssertConnectionIsOpen ();
277 AssertObjectNotDisposed ();
285 return Erase (0, Length);
288 public long Erase (long offset, long amount)
290 if (offset < 0 || amount < 0)
291 throw new ArgumentOutOfRangeException ("Must be a positive value.");
292 if (offset + amount > Length)
293 throw new ArgumentOutOfRangeException ();
295 AssertAmountIsValid (offset, "offset");
296 AssertAmountIsValid (amount, "amount");
298 return (long) locator.Erase ((uint) offset + 1, (uint) amount);
301 public override void Flush ()
306 public override int Read (byte[] buffer, int offset, int count)
309 throw new ArgumentNullException ();
311 AssertAmountIsValid (offset, "offset");
312 AssertAmountIsValid (count, "count");
313 AssertConnectionIsOpen ();
314 AssertObjectNotDisposed ();
317 byte[] output = new byte[count];
319 bytesRead = locator.Read (output, (uint) Position + 1, (uint) count, LobType == OracleType.Blob);
320 output.CopyTo (buffer, offset);
321 position += bytesRead;
326 public override long Seek (long offset, SeekOrigin origin)
328 long newPosition = position;
331 case SeekOrigin.Begin:
332 newPosition = offset;
334 case SeekOrigin.Current:
335 newPosition += offset;
338 newPosition = Length + offset;
342 if (newPosition > Length)
343 throw new ArgumentOutOfRangeException ();
345 position = newPosition;
350 public override void SetLength (long value)
352 AssertAmountIsValid (value, "value");
353 AssertTransactionExists ();
354 AssertConnectionIsOpen ();
355 AssertObjectNotDisposed ();
357 locator.Trim ((uint) value);
361 public override void Write (byte[] buffer, int offset, int count)
364 throw new ArgumentNullException ("Buffer is null.");
365 if (offset < 0 || count < 0)
366 throw new ArgumentOutOfRangeException ("Must be a positive value.");
367 if (offset + count > buffer.Length)
368 throw new ArgumentOutOfRangeException ("The offset and count values specified exceed the buffer provided.");
369 AssertAmountIsValid (offset, "offset");
370 AssertAmountIsValid (count, "count");
371 AssertTransactionExists ();
372 AssertConnectionIsOpen ();
373 AssertObjectNotDisposed ();
376 if (offset + count == buffer.Length && offset == 0)
379 value = new byte[count];
380 Array.Copy (buffer, offset, value, 0, count);
383 position += locator.Write (value, (uint) Position + 1, (uint) value.Length, LobType);
386 #endregion // Methods