2009-10-19 Jb Evain <jbevain@novell.com>
[mcs.git] / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / BlobBase.cs
blobb828e8d386d057ccb0b3c579108c12678f96b5d2
1 /*
2 * Firebird ADO.NET Data provider for .NET and Mono
3 *
4 * The contents of this file are subject to the Initial
5 * Developer's Public License Version 1.0 (the "License");
6 * you may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10 * Software distributed under the License is distributed on
11 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12 * express or implied. See the License for the specific
13 * language governing rights and limitations under the License.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
19 using System;
20 using System.Data;
21 using System.Text;
22 using System.IO;
23 using System.Collections;
25 namespace FirebirdSql.Data.Common
27 internal abstract class BlobBase
29 #region Fields
31 private int rblFlags;
32 private Charset charset;
33 private int segmentSize;
35 #endregion
37 #region Protected Fields
39 protected long blobId;
40 protected int blobHandle;
41 protected int position;
42 protected ITransaction transaction;
44 #endregion
46 #region Properties
48 public int Handle
50 get { return this.blobHandle; }
53 public long Id
55 get { return this.blobId; }
58 public bool EOF
60 get { return (this.rblFlags & IscCodes.RBL_eof_pending) != 0; }
63 #endregion
65 #region Protected Properties
67 protected int SegmentSize
69 get { return this.segmentSize; }
72 #endregion
74 #region Abstract Properties
76 public abstract IDatabase DB
78 get;
81 #endregion
83 #region Constructors
85 protected BlobBase(IDatabase db)
87 this.segmentSize = db.PacketSize;
88 this.charset = db.Charset;
91 #endregion
93 #region Protected Abstract Methods
95 protected abstract void Create();
96 protected abstract void Open();
97 protected abstract byte[] GetSegment();
98 protected abstract void PutSegment(byte[] buffer);
99 protected abstract void Seek(int position);
100 protected abstract void GetBlobInfo();
101 protected abstract void Close();
102 protected abstract void Cancel();
104 #endregion
106 #region Methods
108 public string ReadString()
110 byte[] buffer = this.Read();
111 return this.charset.GetString(buffer, 0, buffer.Length);
114 public byte[] Read()
116 MemoryStream ms = new MemoryStream();
120 this.Open();
122 while (!EOF)
124 byte[] segment = this.GetSegment();
125 ms.Write(segment, 0, segment.Length);
128 this.Close();
130 catch (Exception)
132 // Cancel the blob and rethrow the exception
133 this.Cancel();
135 throw;
138 return ms.ToArray();
141 public void Write(string data)
143 this.Write(this.charset.GetBytes(data));
146 public void Write(byte[] buffer)
148 this.Write(buffer, 0, buffer.Length);
151 public void Write(byte[] buffer, int index, int count)
155 this.Create();
157 byte[] tmpBuffer = null;
159 int length = count;
160 int offset = index;
161 int chunk = length >= this.segmentSize ? this.segmentSize : length;
163 tmpBuffer = new byte[chunk];
165 while (length > 0)
167 if (chunk > length)
169 chunk = (int)length;
170 tmpBuffer = new byte[chunk];
173 Array.Copy(buffer, offset, tmpBuffer, 0, chunk);
174 this.PutSegment(tmpBuffer);
176 offset += chunk;
177 length -= chunk;
180 this.Close();
182 catch (Exception)
184 // Cancel the blob and rethrow the exception
185 this.Cancel();
187 throw;
191 #endregion
193 #region Protected Methods
195 protected void RblAddValue(int rblValue)
197 this.rblFlags |= rblValue;
200 protected void RblRemoveValue(int rblValue)
202 this.rblFlags &= ~rblValue;
205 #endregion