KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > MarkedLimitInputStream


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.MarkedLimitInputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import java.io.DataInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29   An input stream whose internal data is in blocks, the format of each block is
30     (boolean isLastBlock, int blockLength, sequence of blockLength bytes)
31   All blocks except for the last block must have isLastBlock set to false.
32   The last block must have isLastBlock set to true.
33
34   This class implements an input stream whose length is limited, yet
35   the creator (writer) of the stream does not need to know the entire length
36   before creating it.
37 */

38
39 public class MarkedLimitInputStream extends org.apache.derby.iapi.services.io.LimitInputStream
40 {
41     protected boolean isLastBlock;
42     protected int blockLength;
43
44
45     public MarkedLimitInputStream(DataInputStream JavaDoc in)
46         throws IOException JavaDoc
47     {
48         super(in);
49         start();
50     }
51
52     private void start()
53         throws IOException JavaDoc
54     {
55         isLastBlock = ((DataInputStream JavaDoc) in).readBoolean();
56         blockLength = ((DataInputStream JavaDoc) in).readInt();
57
58         if (SanityManager.DEBUG)
59         {
60             if (!isLastBlock)
61             {
62                 SanityManager.ASSERT(blockLength > 0);
63             }
64             else
65             {
66                 SanityManager.ASSERT(blockLength >= 0, "blockLength " + blockLength + " is negative");
67             }
68         }
69         setLimit(blockLength);
70
71     }
72
73     public int read()
74         throws IOException JavaDoc
75     {
76         int i = super.read();
77         if (i == -1)
78         {
79             if (isLastBlock)
80             {
81                 return -1;
82             }
83             else
84             {
85                 start();
86                 return this.read();
87             }
88         }
89         else
90         {
91             return i;
92         }
93     }
94
95
96     public int read(byte b[], int off, int len)
97         throws IOException JavaDoc
98     {
99         if (isLastBlock)
100         {
101             // get as many bytes as we can, superclass may return less
102
// bytes than we asked for without good reason
103
int m = 0;
104             while (m < len)
105             {
106                 int count = super.read(b, off + m, len - m);
107                 if (count < 0)
108                 {
109                     break;
110                 }
111                 m += count;
112             }
113             return m;
114         }
115
116         // read until either get back all the bytes we asked for
117
// or until we get -1
118
int n = 0;
119         while (n < len)
120         {
121             int count = super.read(b, off + n, len - n);
122             if (count < 0)
123             {
124                 break;
125             }
126             n += count;
127         }
128
129         if (SanityManager.DEBUG)
130         {
131             SanityManager.ASSERT(n <= len);
132         }
133         if (n == len)
134         {
135             return n;
136         }
137
138         // n < len, we didn't finish yet
139
// init next block
140
start();
141         // read rest
142
if (n < 0)
143         {
144             return this.read(b,off,len);
145         }
146
147         return n + this.read(b, off+n, len-n);
148     }
149
150
151     public long skip(long count)
152         throws IOException JavaDoc
153     {
154         if (isLastBlock)
155         {
156             return super.skip(count);
157         }
158
159         // long n = super.skip(count);
160
// read until either skip all the bytes we asked to
161
// or until we get a result which is <= 0
162
long n = 0;
163         while (n < count)
164         {
165             long c = super.skip(count-n);
166             if (c <= 0)
167             {
168                 break;
169             }
170             n += c;
171         }
172
173         if (SanityManager.DEBUG)
174         {
175             SanityManager.ASSERT(n <= count);
176         }
177         if (n == count)
178         {
179             return n;
180         }
181         // if n < count, we didn't finish skipping yet
182
// init next block
183
start();
184         // read rest
185
if (n < 0)
186         {
187             return this.skip(count);
188         }
189         return n + this.skip(count-n);
190     }
191
192
193 }
194
Popular Tags