001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.data.ByteHolder
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.impl.store.raw.data;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029: import java.util.Vector;
030:
031: /**
032: Holder for a growing sequence of bytes. The ByteHolder supports a
033: writing phase in which a caller appends bytes to the ByteHolder.
034: Later the caller may read the bytes out of the ByteHolder in
035: the order they were written.
036: */
037: public interface ByteHolder {
038: /**
039: Write a byte to this ByteHolder.
040:
041: <P>The ByteHolder must be in writing mode to call this.
042: */
043: public void write(int b) throws IOException;
044:
045: /**
046: Write len bytes of data starting at 'offset' to this ByteHolder.
047:
048: <P>The ByteHolder must be in writing mode to call this.
049: */
050: public void write(byte[] data, int offset, int len)
051: throws IOException;
052:
053: /**
054: Write up to count bytes from an input stream to this
055: ByteHolder. This may write fewer bytes if it encounters
056: an end of file on the input stream.
057:
058: @return the number of bytes written.
059: @exception IOException thrown when reading in causes an
060: error.
061: */
062: public long write(InputStream in, long count) throws IOException;
063:
064: /**
065: Clear the bytes from the ByteHolder and place it in writing
066: mode. This may not free the memory the ByteHolder uses to
067: store data.
068: */
069: public void clear() throws IOException;
070:
071: /**
072: Place a ByteHolder in reading mode. After this call,
073: reads scan bytes sequentially in the order they were
074: written to the ByteHolder starting from the first byte.
075: When the ByteHolder is already in readmode this simply
076: arranges for reads to start at the beginning of the
077: sequence of saved bytes.
078: */
079: public void startReading() throws IOException;
080:
081: /**
082: Read a byte from this ByteHolder.
083:
084: <P>The ByteHolder must be in reading mode to call this.
085:
086: @return The byte or -1 if there are no bytes available.
087: */
088: public int read() throws IOException;
089:
090: /**
091: Read up to 'len' bytes from this ByteHolder and store them in
092: an array at offset 'off'.
093:
094: <P>The ByteHolder must be in reading mode to call this.
095:
096: @return the number of bytes read or -1 if the this ByteHolder
097: has no more bytes.
098: */
099: public int read(byte b[], int off, int len) throws IOException;
100:
101: /**
102: Read from the ByteHolder.
103: <p>
104: Read up to 'len' bytes from this ByteHolder and write them to
105: the OutputStream
106:
107: <P>The ByteHolder must be in reading mode to call this.
108:
109: @return the number of bytes read or -1 if the this ByteHolder
110: has no more bytes.
111: */
112: public int read(OutputStream out, int len) throws IOException;
113:
114: /**
115: shift the remaining unread bytes to the beginning of the byte holder
116: */
117: public int shiftToFront() throws IOException;
118:
119: /**
120: Return the number of bytes that can be read from this ByteHolder
121: without blocking on an IO.
122: */
123: public int available() throws IOException;
124:
125: /**
126: Return the number of bytes that have been saved to this byte holder.
127: This result is different from available() as it is unaffected by the
128: current read position on the ByteHolder.
129: */
130: public int numBytesSaved() throws IOException;
131:
132: /**
133: Skip over the specified number of bytes in a ByteHolder.
134: */
135: public long skip(long count) throws IOException;
136:
137: /**
138: Return true if this is in writing mode.
139: */
140: public boolean writingMode();
141: }
|