001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.ArrayOutputStream
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.io;
023:
024: import java.io.IOException;
025: import java.io.EOFException;
026: import java.io.OutputStream;
027:
028: public class ArrayOutputStream extends OutputStream implements Limit {
029:
030: private byte[] pageData;
031:
032: private int start;
033: private int end; // exclusive
034: private int position;
035:
036: public ArrayOutputStream() {
037: super ();
038: }
039:
040: public ArrayOutputStream(byte[] data) {
041: super ();
042: setData(data);
043: }
044:
045: public void setData(byte[] data) {
046: pageData = data;
047: start = 0;
048: if (data != null)
049: end = data.length;
050: else
051: end = 0;
052: position = 0;
053:
054: }
055:
056: /*
057: ** Methods of OutputStream
058: */
059:
060: public void write(int b) throws IOException {
061: if (position >= end)
062: throw new EOFException();
063:
064: pageData[position++] = (byte) b;
065:
066: }
067:
068: public void write(byte b[], int off, int len) throws IOException {
069:
070: if ((position + len) > end)
071: throw new EOFException();
072:
073: System.arraycopy(b, off, pageData, position, len);
074: position += len;
075: }
076:
077: /*
078: ** Methods of LengthOutputStream
079: */
080:
081: public int getPosition() {
082: return position;
083: }
084:
085: /**
086: Set the position of the stream pointer.
087: */
088: public void setPosition(int newPosition) throws IOException {
089: if ((newPosition < start) || (newPosition > end))
090: throw new EOFException();
091:
092: position = newPosition;
093: }
094:
095: public void setLimit(int length) throws IOException {
096:
097: if (length < 0) {
098: throw new EOFException();
099: }
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: }
|