~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit/ObjectStream.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
5
This program and the accompanying materials are made available
 
6
under the terms of the Eclipse Distribution License v1.0 which
 
7
accompanies this distribution, is reproduced below, and is
 
8
available at http://www.eclipse.org/org/documents/edl-v10.php
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
19
- Redistributions in binary form must reproduce the above
 
20
  copyright notice, this list of conditions and the following
 
21
  disclaimer in the documentation and/or other materials provided
 
22
  with the distribution.
 
23
 
 
24
- Neither the name of the Eclipse Foundation, Inc. nor the
 
25
  names of its contributors may be used to endorse or promote
 
26
  products derived from this software without specific prior
 
27
  written permission.
 
28
 
 
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
42
*/
 
43
 
 
44
using System;
 
45
using NGit;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit
 
49
{
 
50
        /// <summary>
 
51
        /// Stream of data coming from an object loaded by
 
52
        /// <see cref="ObjectLoader">ObjectLoader</see>
 
53
        /// .
 
54
        /// </summary>
 
55
        public abstract class ObjectStream : InputStream
 
56
        {
 
57
                /// <returns>
 
58
                /// Git object type, see
 
59
                /// <see cref="Constants">Constants</see>
 
60
                /// .
 
61
                /// </returns>
 
62
                public abstract int GetType();
 
63
 
 
64
                /// <returns>total size of object in bytes</returns>
 
65
                public abstract long GetSize();
 
66
 
 
67
                /// <summary>Simple stream around the cached byte array created by a loader.</summary>
 
68
                /// <remarks>
 
69
                /// Simple stream around the cached byte array created by a loader.
 
70
                /// <p>
 
71
                /// ObjectLoader implementations can use this stream type when the object's
 
72
                /// content is small enough to be accessed as a single byte array, but the
 
73
                /// application has still requested it in stream format.
 
74
                /// </remarks>
 
75
                public class SmallStream : ObjectStream
 
76
                {
 
77
                        private readonly int type;
 
78
 
 
79
                        private readonly byte[] data;
 
80
 
 
81
                        private int ptr;
 
82
 
 
83
                        private int mark;
 
84
 
 
85
                        /// <summary>Create the stream from an existing loader's cached bytes.</summary>
 
86
                        /// <remarks>Create the stream from an existing loader's cached bytes.</remarks>
 
87
                        /// <param name="loader">the loader.</param>
 
88
                        public SmallStream(ObjectLoader loader) : this(loader.GetType(), loader.GetCachedBytes
 
89
                                ())
 
90
                        {
 
91
                        }
 
92
 
 
93
                        /// <summary>Create the stream from an existing byte array and type.</summary>
 
94
                        /// <remarks>Create the stream from an existing byte array and type.</remarks>
 
95
                        /// <param name="type">the type constant for the object.</param>
 
96
                        /// <param name="data">the fully inflated content of the object.</param>
 
97
                        public SmallStream(int type, byte[] data)
 
98
                        {
 
99
                                this.type = type;
 
100
                                this.data = data;
 
101
                        }
 
102
 
 
103
                        public override int GetType()
 
104
                        {
 
105
                                return type;
 
106
                        }
 
107
 
 
108
                        public override long GetSize()
 
109
                        {
 
110
                                return data.Length;
 
111
                        }
 
112
 
 
113
                        public override int Available()
 
114
                        {
 
115
                                return data.Length - ptr;
 
116
                        }
 
117
 
 
118
                        public override long Skip(long n)
 
119
                        {
 
120
                                int s = (int)Math.Min(Available(), Math.Max(0, n));
 
121
                                ptr += s;
 
122
                                return s;
 
123
                        }
 
124
 
 
125
                        public override int Read()
 
126
                        {
 
127
                                if (ptr == data.Length)
 
128
                                {
 
129
                                        return -1;
 
130
                                }
 
131
                                return data[ptr++] & unchecked((int)(0xff));
 
132
                        }
 
133
 
 
134
                        public override int Read(byte[] b, int off, int len)
 
135
                        {
 
136
                                if (ptr == data.Length)
 
137
                                {
 
138
                                        return -1;
 
139
                                }
 
140
                                int n = Math.Min(Available(), len);
 
141
                                System.Array.Copy(data, ptr, b, off, n);
 
142
                                ptr += n;
 
143
                                return n;
 
144
                        }
 
145
 
 
146
                        public override bool MarkSupported()
 
147
                        {
 
148
                                return true;
 
149
                        }
 
150
 
 
151
                        public override void Mark(int readlimit)
 
152
                        {
 
153
                                mark = ptr;
 
154
                        }
 
155
 
 
156
                        public override void Reset()
 
157
                        {
 
158
                                ptr = mark;
 
159
                        }
 
160
                }
 
161
 
 
162
                /// <summary>Simple filter stream around another stream.</summary>
 
163
                /// <remarks>
 
164
                /// Simple filter stream around another stream.
 
165
                /// <p>
 
166
                /// ObjectLoader implementations can use this stream type when the object's
 
167
                /// content is available from a standard InputStream.
 
168
                /// </remarks>
 
169
                public class Filter : ObjectStream
 
170
                {
 
171
                        private readonly int type;
 
172
 
 
173
                        private readonly long size;
 
174
 
 
175
                        private readonly InputStream @in;
 
176
 
 
177
                        /// <summary>Create a filter stream for an object.</summary>
 
178
                        /// <remarks>Create a filter stream for an object.</remarks>
 
179
                        /// <param name="type">the type of the object.</param>
 
180
                        /// <param name="size">total size of the object, in bytes.</param>
 
181
                        /// <param name="in">
 
182
                        /// stream the object's raw data is available from. This
 
183
                        /// stream should be buffered with some reasonable amount of
 
184
                        /// buffering.
 
185
                        /// </param>
 
186
                        public Filter(int type, long size, InputStream @in)
 
187
                        {
 
188
                                this.type = type;
 
189
                                this.size = size;
 
190
                                this.@in = @in;
 
191
                        }
 
192
 
 
193
                        public override int GetType()
 
194
                        {
 
195
                                return type;
 
196
                        }
 
197
 
 
198
                        public override long GetSize()
 
199
                        {
 
200
                                return size;
 
201
                        }
 
202
 
 
203
                        /// <exception cref="System.IO.IOException"></exception>
 
204
                        public override int Available()
 
205
                        {
 
206
                                return @in.Available();
 
207
                        }
 
208
 
 
209
                        /// <exception cref="System.IO.IOException"></exception>
 
210
                        public override long Skip(long n)
 
211
                        {
 
212
                                return @in.Skip(n);
 
213
                        }
 
214
 
 
215
                        /// <exception cref="System.IO.IOException"></exception>
 
216
                        public override int Read()
 
217
                        {
 
218
                                return @in.Read();
 
219
                        }
 
220
 
 
221
                        /// <exception cref="System.IO.IOException"></exception>
 
222
                        public override int Read(byte[] b, int off, int len)
 
223
                        {
 
224
                                return @in.Read(b, off, len);
 
225
                        }
 
226
 
 
227
                        public override bool MarkSupported()
 
228
                        {
 
229
                                return @in.MarkSupported();
 
230
                        }
 
231
 
 
232
                        public override void Mark(int readlimit)
 
233
                        {
 
234
                                @in.Mark(readlimit);
 
235
                        }
 
236
 
 
237
                        /// <exception cref="System.IO.IOException"></exception>
 
238
                        public override void Reset()
 
239
                        {
 
240
                                @in.Reset();
 
241
                        }
 
242
 
 
243
                        /// <exception cref="System.IO.IOException"></exception>
 
244
                        public override void Close()
 
245
                        {
 
246
                                @in.Close();
 
247
                        }
 
248
                }
 
249
        }
 
250
}