DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UrlConfigSource.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.Collections.Specialized;
19 using System.IO;
20 using System.Net;
21 
22 namespace Deveel.Data.Configuration {
23  public sealed class UrlConfigSource : IConfigSource {
24  public UrlConfigSource(Uri url) {
25  Url = url;
26  }
27 
28  public UrlConfigSource(string url)
29  : this(new Uri(url)) {
30  }
31 
32  public Uri Url { get; private set; }
33 
34  public Stream InputStream {
35  get {
36  try {
37  using (var client = new WebClient()) {
38  var data = client.DownloadData(Url);
39  return new MemoryStream(data, false);
40  }
41  } catch (Exception ex) {
42  throw new DatabaseConfigurationException(String.Format("Could not load data from the url '{0}'.", Url), ex);
43  }
44  }
45  }
46 
47  public Stream OutputStream {
48  get { return new WebOutputStream(Url); }
49  }
50 
51  #region WebOutputStream
52 
53  class WebOutputStream : Stream {
54  private readonly Uri url;
55  private readonly MemoryStream baseStream;
56  private bool closed;
57 
58  public WebOutputStream(Uri url) {
59  this.url = url;
60  baseStream = new MemoryStream(1024);
61  }
62 
63  private void AssertNotClosed() {
64  if (closed)
65  throw new IOException("The stream is closed and cannot be written.");
66  }
67 
68  public override void Flush() {
69  AssertNotClosed();
70 
71  lock (baseStream) {
72  baseStream.Flush();
73 
74  var data = baseStream.ToArray();
75  using (var client = new WebClient()) {
76  client.UploadData(url, data);
77  }
78  }
79  }
80 
81  public override long Seek(long offset, SeekOrigin origin) {
82  AssertNotClosed();
83 
84  lock (baseStream) {
85  return baseStream.Seek(offset, origin);
86  }
87  }
88 
89  public override void SetLength(long value) {
90  AssertNotClosed();
91 
92  lock (baseStream) {
93  baseStream.SetLength(value);
94  }
95  }
96 
97  public override int Read(byte[] buffer, int offset, int count) {
98  throw new NotSupportedException();
99  }
100 
101  public override void Write(byte[] buffer, int offset, int count) {
102  AssertNotClosed();
103 
104  lock (baseStream) {
105  baseStream.Write(buffer, offset, count);
106  }
107  }
108 
109  public override bool CanRead {
110  get { return false; }
111  }
112 
113  public override bool CanSeek {
114  get { return true; }
115  }
116 
117  public override bool CanWrite {
118  get { return false; }
119  }
120 
121  public override long Length {
122  get {
123  lock (baseStream) {
124  return baseStream.Length;
125  }
126  }
127  }
128 
129  public override long Position {
130  get {
131  lock (baseStream) {
132  return baseStream.Position;
133  }
134  }
135  set {
136  AssertNotClosed();
137  lock (baseStream) {
138  baseStream.Position = value;
139  }
140  }
141  }
142 
143  public override void Close() {
144  try {
145  baseStream.Close();
146  base.Close();
147  } finally {
148  closed = true;
149  }
150  }
151  }
152 
153  #endregion
154  }
155 }
override long Seek(long offset, SeekOrigin origin)
A source for stored configurations or destination to configurations to store.
override int Read(byte[] buffer, int offset, int count)
override void Write(byte[] buffer, int offset, int count)