20 namespace Deveel.Data.Store {
29 private const int BufferSize = 64*1024;
32 if (largeObject == null)
33 throw new ArgumentNullException(
"largeObject");
35 this.largeObject = largeObject;
36 outTempStream =
new MemoryStream(64*1024);
38 readBuf =
new byte[BufferSize];
43 largeObject.Read(pos, buffer, length);
47 long readPos = (pos/BufferSize)*BufferSize;
48 int toRead = (int)
System.Math.Min(BufferSize, (largeObject.RawSize - readPos));
50 ReadPageContent(readBuf, readPos, toRead);
51 readBufferPos = readPos;
55 protected override void Dispose(
bool disposing) {
57 if (outTempStream != null)
58 outTempStream.Dispose();
62 base.Dispose(disposing);
66 if (outTempStream == null ||
67 outTempStream.Length == 0)
72 var buffer =
new byte[BufferSize];
73 var totalLength = outTempStream.Length;
75 outTempStream.Seek(0, SeekOrigin.Begin);
77 while (offset < totalLength) {
80 int blockRead = (int)
System.Math.Min((
long) BufferSize, (totalLength - offset));
81 int toRead = blockRead;
83 int count = outTempStream.Read(buffer, index, toRead);
92 largeObject.Write(offset, buffer, blockRead);
97 outTempStream.SetLength(0);
101 public override long Seek(
long offset, SeekOrigin origin) {
102 throw new NotImplementedException();
106 throw new NotSupportedException(
"The lenght of the underlying object cannot be changed.");
109 public override int Read(byte[] buffer,
int offset,
int count) {
110 if (!largeObject.IsComplete)
111 throw new IOException(
"The underlying object is not complete.");
117 if (readBufferPos == -1) {
118 FillBuffer(position);
121 int p = (int) (position - readBufferPos);
122 long bufferEnd =
System.Math.Min(readBufferPos + BufferSize, largeObject.RawSize);
123 int toRead = (int)
System.Math.Min((
long) count, bufferEnd - position);
129 Array.Copy(readBuf, p, buffer, offset, toRead);
135 if (p >= BufferSize) {
136 FillBuffer(readBufferPos + BufferSize);
139 bufferEnd =
System.Math.Min(readBufferPos + BufferSize, largeObject.RawSize);
140 toRead = (int)
System.Math.Min((
long)count, bufferEnd - position);
146 public override void Write(byte[] buffer,
int offset,
int count) {
147 if (largeObject.IsComplete)
148 throw new IOException(
"The underlying object is complete.");
150 outTempStream.Write(buffer, offset, count);
154 public override bool CanRead {
155 get {
return largeObject.IsComplete; }
158 public override bool CanSeek {
162 public override bool CanWrite {
163 get {
return !largeObject.IsComplete; }
166 public override long Length {
167 get {
return largeObject.RawSize; }
170 public override long Position {
171 get {
return position; }
172 set { position = Seek(value, SeekOrigin.Begin); }
override void SetLength(long value)
readonly ILargeObject largeObject
override long Seek(long offset, SeekOrigin origin)
Defines a referenced object that can be accessed on a multi-phase level.
void ReadPageContent(byte[] buffer, long pos, int length)
void FillBuffer(long pos)
override void Write(byte[] buffer, int offset, int count)
override int Read(byte[] buffer, int offset, int count)
override void Dispose(bool disposing)
ObjectStream(ILargeObject largeObject)