1
package org.postgresql.largeobject;
3
import java.io.InputStream;
4
import java.io.IOException;
5
import java.sql.SQLException;
8
* This is an initial implementation of an InputStream from a large object.
9
* For now, the bare minimum is implemented. Later (after 7.1) we will overide
10
* the other read methods to optimise them.
12
public class BlobInputStream extends InputStream
15
* The parent LargeObject
17
private LargeObject lo;
20
* Buffer used to improve performance
22
private byte[] buffer;
25
* Position within buffer
40
* @param lo LargeObject to read from
42
public BlobInputStream(LargeObject lo)
48
* @param lo LargeObject to read from
49
* @param bsize buffer size
51
public BlobInputStream(LargeObject lo, int bsize)
60
* The minimum required to implement input stream
62
public int read() throws java.io.IOException
66
if (buffer == null || bpos >= buffer.length)
68
buffer = lo.read(bsize);
73
if (bpos >= buffer.length)
78
int ret = (buffer[bpos] & 0x7F);
79
if ((buffer[bpos] &0x80) == 0x80)
88
catch (SQLException se)
90
throw new IOException(se.toString());
96
* Closes this input stream and releases any system resources associated
99
* <p> The <code>close</code> method of <code>InputStream</code> does
102
* @exception IOException if an I/O error occurs.
104
public void close() throws IOException
111
catch (SQLException se)
113
throw new IOException(se.toString());
118
* Marks the current position in this input stream. A subsequent call to
119
* the <code>reset</code> method repositions this stream at the last marked
120
* position so that subsequent reads re-read the same bytes.
122
* <p> The <code>readlimit</code> arguments tells this input stream to
123
* allow that many bytes to be read before the mark position gets
126
* <p> The general contract of <code>mark</code> is that, if the method
127
* <code>markSupported</code> returns <code>true</code>, the stream somehow
128
* remembers all the bytes read after the call to <code>mark</code> and
129
* stands ready to supply those same bytes again if and whenever the method
130
* <code>reset</code> is called. However, the stream is not required to
131
* remember any data at all if more than <code>readlimit</code> bytes are
132
* read from the stream before <code>reset</code> is called.
134
* <p> The <code>mark</code> method of <code>InputStream</code> does
137
* @param readlimit the maximum limit of bytes that can be read before
138
* the mark position becomes invalid.
139
* @see java.io.InputStream#reset()
141
public synchronized void mark(int readlimit)
147
catch (SQLException se)
149
//throw new IOException(se.toString());
154
* Repositions this stream to the position at the time the
155
* <code>mark</code> method was last called on this input stream.
156
* NB: If mark is not called we move to the begining.
157
* @see java.io.InputStream#mark(int)
158
* @see java.io.IOException
160
public synchronized void reset()
167
catch (SQLException se)
169
throw new IOException(se.toString());
174
* Tests if this input stream supports the <code>mark</code> and
175
* <code>reset</code> methods. The <code>markSupported</code> method of
176
* <code>InputStream</code> returns <code>false</code>.
178
* @return <code>true</code> if this true type supports the mark and reset
179
* method; <code>false</code> otherwise.
180
* @see java.io.InputStream#mark(int)
181
* @see java.io.InputStream#reset()
183
public boolean markSupported()