DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
AreaStream.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.IO;
19 
20 namespace Deveel.Data.Store {
21  public sealed class AreaStream : Stream {
22  private readonly IArea area;
23 
24  public AreaStream(IArea area) {
25  if (area == null)
26  throw new ArgumentNullException("area");
27 
28  this.area = area;
29  }
30 
31  public override void Flush() {
32  area.Flush();
33  }
34 
35  public override long Seek(long offset, SeekOrigin origin) {
36  if (origin == SeekOrigin.Begin) {
37  if (offset >= Length)
38  throw new ArgumentOutOfRangeException("offset");
39 
40  area.Position = (int) offset;
41  } else if (origin == SeekOrigin.Current) {
42  var pos = area.Position;
43  var length = area.Length;
44  if (pos + offset >= length)
45  throw new ArgumentOutOfRangeException("offset");
46 
47  area.Position = pos + (int)offset;
48  } else {
49  var length = area.Length;
50  var newPos = length - offset;
51  if (newPos < 0)
52  throw new ArgumentOutOfRangeException("offset");
53 
54  area.Position = (int)newPos;
55  }
56 
57  return area.Position;
58  }
59 
60  public override void SetLength(long value) {
61  throw new NotSupportedException();
62  }
63 
64  public override int Read(byte[] buffer, int offset, int count) {
65  return area.Read(buffer, offset, count);
66  }
67 
68  public override void Write(byte[] buffer, int offset, int count) {
69  area.Write(buffer, offset, count);
70  }
71 
72  public override bool CanRead {
73  get { return true; }
74  }
75 
76  public override bool CanSeek {
77  get { return true; }
78  }
79 
80  public override bool CanWrite {
81  get { return !area.IsReadOnly; }
82  }
83 
84  public override long Length {
85  get { return area.Length; }
86  }
87 
88  public override long Position {
89  get { return area.Position; }
90  set { Seek(value, SeekOrigin.Begin); }
91  }
92  }
93 }
override void SetLength(long value)
Definition: AreaStream.cs:60
An interface for access the contents of an area of a store.
Definition: IArea.cs:23
override long Seek(long offset, SeekOrigin origin)
Definition: AreaStream.cs:35
override void Write(byte[] buffer, int offset, int count)
Definition: AreaStream.cs:68
override int Read(byte[] buffer, int offset, int count)
Definition: AreaStream.cs:64