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, 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 = new byte [Length];
146 Read (buffer, 1, (int) Length);
148 if (LobType == OracleType.Clob)
149 return (new UnicodeEncoding ()).GetString (buffer);
154 #endregion // Properties
159 public void Append (OracleLob source)
162 throw new ArgumentNullException ();
163 if (Connection.State == ConnectionState.Closed)
164 throw new InvalidOperationException ();
165 throw new NotImplementedException ();
168 void AssertAmountIsEven (long amount, string argName)
171 throw new ArgumentOutOfRangeException ("CLOB and NCLOB parameters require even number of bytes for this argument.");
174 void AssertAmountIsValid (long amount, string argName)
176 if (amount > UInt32.MaxValue)
177 throw new ArgumentOutOfRangeException ("Argument too big.");
178 if (LobType == OracleType.Clob || LobType == OracleType.NClob)
179 AssertAmountIsEven (amount, argName);
182 void AssertConnectionIsOpen ()
184 if (connection.State == ConnectionState.Closed)
185 throw new InvalidOperationException ("Invalid operation. The connection is closed.");
188 void AssertObjectNotDisposed ()
191 throw new ObjectDisposedException ("OracleLob");
194 void AssertTransactionExists ()
196 if (connection.Transaction == null)
197 throw new InvalidOperationException ("Modifying a LOB requires that the connection be transacted.");
200 public void BeginBatch ()
202 BeginBatch (OracleLobOpenMode.ReadOnly);
205 public void BeginBatch (OracleLobOpenMode mode)
207 AssertConnectionIsOpen ();
208 AssertObjectNotDisposed ();
210 locator.BeginBatch (mode);
215 public object Clone ()
217 throw new NotImplementedException ();
221 public override void Close ()
227 public long CopyTo (OracleLob destination)
229 return CopyTo (0, destination, 0, Length);
232 public long CopyTo (OracleLob destination, long destinationOffset)
234 return CopyTo (0, destination, destinationOffset, Length);
237 public long CopyTo (long sourceOffset, OracleLob destination, long destinationOffset, long amount)
239 if (destination.IsNull)
240 throw new ArgumentNullException ();
242 AssertAmountIsValid (sourceOffset, "sourceOffset");
243 AssertAmountIsValid (destinationOffset, "destinationOffset");
244 AssertAmountIsValid (amount, "amount");
245 AssertTransactionExists ();
246 AssertConnectionIsOpen ();
248 return (long) locator.Copy (destination.Locator, (uint) amount, (uint) destinationOffset + 1, (uint) sourceOffset + 1);
252 public void Dispose ()
254 throw new NotImplementedException ();
257 public void EndBatch ()
259 AssertConnectionIsOpen ();
260 AssertObjectNotDisposed ();
268 return Erase (1, Length);
271 public long Erase (long offset, long amount)
273 if (offset < 0 || amount < 0)
274 throw new ArgumentOutOfRangeException ("Must be a positive value.");
275 if (offset + amount > Length)
276 throw new ArgumentOutOfRangeException ();
278 AssertAmountIsValid (offset, "offset");
279 AssertAmountIsValid (amount, "amount");
281 return (long) locator.Erase ((uint) offset + 1, (uint) amount);
284 public override void Flush ()
289 public override int Read (byte[] buffer, int offset, int count)
292 throw new ArgumentNullException ();
294 AssertAmountIsValid (offset, "offset");
295 AssertAmountIsValid (count, "count");
296 AssertConnectionIsOpen ();
297 AssertObjectNotDisposed ();
300 byte[] output = new byte[count];
302 bytesRead = locator.Read (output, (uint) Position, (uint) count, LobType == OracleType.Blob);
303 output.CopyTo (buffer, offset);
304 position += bytesRead;
309 public override long Seek (long offset, SeekOrigin origin)
311 long newPosition = position;
314 case SeekOrigin.Begin:
315 newPosition = offset;
317 case SeekOrigin.Current:
318 newPosition += offset;
321 newPosition = Length - offset;
325 if (newPosition > Length)
326 throw new ArgumentOutOfRangeException ();
328 position = newPosition;
333 public override void SetLength (long value)
335 AssertAmountIsValid (value, "value");
336 AssertTransactionExists ();
337 AssertConnectionIsOpen ();
338 AssertObjectNotDisposed ();
340 locator.Trim ((uint) value);
344 public override void Write (byte[] buffer, int offset, int count)
347 throw new ArgumentNullException ("Buffer is null.");
348 if (offset < 0 || count < 0)
349 throw new ArgumentOutOfRangeException ("Must be a positive value.");
350 if (offset + count > buffer.Length)
351 throw new ArgumentOutOfRangeException ("The offset and count values specified exceed the buffer provided.");
352 AssertAmountIsValid (offset, "offset");
353 AssertAmountIsValid (count, "count");
354 AssertTransactionExists ();
355 AssertConnectionIsOpen ();
356 AssertObjectNotDisposed ();
359 if (offset + count == buffer.Length && offset == 0)
362 value = new byte[count];
363 Array.Copy (buffer, offset, value, 0, count);
366 position += locator.Write (value, (uint) Position, (uint) value.Length, LobType);
369 #endregion // Methods