KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > ArrayOutputStream


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.ArrayOutputStream
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.derby.iapi.services.io;
23
24 import java.io.IOException JavaDoc;
25 import java.io.EOFException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 public class ArrayOutputStream extends OutputStream JavaDoc implements Limit {
29
30     private byte[] pageData;
31
32     private int start;
33     private int end; // exclusive
34
private int position;
35
36     public ArrayOutputStream() {
37         super();
38     }
39
40     public ArrayOutputStream(byte[] data) {
41         super();
42         setData(data);
43     }
44
45     public void setData(byte[] data) {
46         pageData = data;
47         start = 0;
48         if (data != null)
49             end = data.length;
50         else
51             end = 0;
52         position = 0;
53
54     }
55
56     /*
57     ** Methods of OutputStream
58     */

59
60     public void write(int b) throws IOException JavaDoc {
61         if (position >= end)
62             throw new EOFException JavaDoc();
63
64         pageData[position++] = (byte) b;
65
66     }
67
68     public void write(byte b[], int off, int len) throws IOException JavaDoc {
69
70         if ((position + len) > end)
71             throw new EOFException JavaDoc();
72
73         System.arraycopy(b, off, pageData, position, len);
74         position += len;
75     }
76
77     /*
78     ** Methods of LengthOutputStream
79     */

80
81     public int getPosition() {
82         return position;
83     }
84
85     /**
86         Set the position of the stream pointer.
87     */

88     public void setPosition(int newPosition)
89         throws IOException JavaDoc {
90         if ((newPosition < start) || (newPosition > end))
91             throw new EOFException JavaDoc();
92
93         position = newPosition;
94     }
95
96     public void setLimit(int length) throws IOException JavaDoc {
97
98         if (length < 0) {
99             throw new EOFException JavaDoc();
100         }
101
102         if ((position + length) > end) {
103             throw new EOFException JavaDoc();
104         }
105
106         start = position;
107         end = position + length;
108
109         return;
110     }
111
112     public int clearLimit() {
113
114         int unwritten = end - position;
115
116         end = pageData.length;
117
118         return unwritten;
119     }
120 }
121
Popular Tags