1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.ArrayOutputStream |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import java.io.IOException; |
24 | import java.io.EOFException; |
25 | import java.io.OutputStream; |
26 | |
27 | public class ArrayOutputStream extends OutputStream implements Limit { |
28 | |
29 | private byte[] pageData; |
30 | |
31 | private int start; |
32 | private int end; // exclusive |
33 | private int position; |
34 | |
35 | public ArrayOutputStream() { |
36 | super(); |
37 | } |
38 | |
39 | public ArrayOutputStream(byte[] data) { |
40 | super(); |
41 | setData(data); |
42 | } |
43 | |
44 | public void setData(byte[] data) { |
45 | pageData = data; |
46 | start = 0; |
47 | if (data != null) |
48 | end = data.length; |
49 | else |
50 | end = 0; |
51 | position = 0; |
52 | |
53 | } |
54 | |
55 | /* |
56 | ** Methods of OutputStream |
57 | */ |
58 | |
59 | public void write(int b) throws IOException { |
60 | if (position >= end) |
61 | throw new EOFException(); |
62 | |
63 | pageData[position++] = (byte) b; |
64 | |
65 | } |
66 | |
67 | public void write(byte b[], int off, int len) throws IOException { |
68 | |
69 | if ((position + len) > end) |
70 | throw new EOFException(); |
71 | |
72 | System.arraycopy(b, off, pageData, position, len); |
73 | position += len; |
74 | } |
75 | |
76 | /* |
77 | ** Methods of LengthOutputStream |
78 | */ |
79 | |
80 | public int getPosition() { |
81 | return position; |
82 | } |
83 | |
84 | /** |
85 | Set the position of the stream pointer. |
86 | */ |
87 | public void setPosition(int newPosition) |
88 | throws IOException { |
89 | if ((newPosition < start) || (newPosition > end)) |
90 | throw new EOFException(); |
91 | |
92 | position = newPosition; |
93 | } |
94 | |
95 | public void setLimit(int length) throws IOException { |
96 | |
97 | if (length < 0) { |
98 | throw new EOFException(); |
99 | } |
100 | |
101 | if ((position + length) > end) { |
102 | throw new EOFException(); |
103 | } |
104 | |
105 | start = position; |
106 | end = position + length; |
107 | |
108 | return; |
109 | } |
110 | |
111 | public int clearLimit() { |
112 | |
113 | int unwritten = end - position; |
114 | |
115 | end = pageData.length; |
116 | |
117 | return unwritten; |
118 | } |
119 | } |