~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/largeobject/BlobInputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.largeobject;
 
2
 
 
3
import java.io.InputStream;
 
4
import java.io.IOException;
 
5
import java.sql.SQLException;
 
6
 
 
7
/*
 
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.
 
11
 */
 
12
public class BlobInputStream extends InputStream
 
13
{
 
14
        /*
 
15
         * The parent LargeObject
 
16
         */
 
17
        private LargeObject lo;
 
18
 
 
19
        /*
 
20
         * Buffer used to improve performance
 
21
         */
 
22
        private byte[] buffer;
 
23
 
 
24
        /*
 
25
         * Position within buffer
 
26
         */
 
27
        private int bpos;
 
28
 
 
29
        /*
 
30
         * The buffer size
 
31
         */
 
32
        private int bsize;
 
33
 
 
34
        /*
 
35
         * The mark position
 
36
         */
 
37
        private int mpos = 0;
 
38
 
 
39
        /*
 
40
         * @param lo LargeObject to read from
 
41
         */
 
42
        public BlobInputStream(LargeObject lo)
 
43
        {
 
44
                this(lo, 1024);
 
45
        }
 
46
 
 
47
        /*
 
48
         * @param lo LargeObject to read from
 
49
         * @param bsize buffer size
 
50
         */
 
51
        public BlobInputStream(LargeObject lo, int bsize)
 
52
        {
 
53
                this.lo = lo;
 
54
                buffer = null;
 
55
                bpos = 0;
 
56
                this.bsize = bsize;
 
57
        }
 
58
 
 
59
        /*
 
60
         * The minimum required to implement input stream
 
61
         */
 
62
        public int read() throws java.io.IOException
 
63
        {
 
64
                try
 
65
                {
 
66
                        if (buffer == null || bpos >= buffer.length)
 
67
                        {
 
68
                                buffer = lo.read(bsize);
 
69
                                bpos = 0;
 
70
                        }
 
71
 
 
72
                        // Handle EOF
 
73
                        if (bpos >= buffer.length)
 
74
                        {
 
75
                                return -1;
 
76
                        }
 
77
 
 
78
                        int ret = (buffer[bpos] & 0x7F);
 
79
                        if ((buffer[bpos] &0x80) == 0x80)
 
80
                        {
 
81
                                ret |= 0x80;
 
82
                        }
 
83
 
 
84
                        bpos++;
 
85
 
 
86
                        return ret;
 
87
                }
 
88
                catch (SQLException se)
 
89
                {
 
90
                        throw new IOException(se.toString());
 
91
                }
 
92
        }
 
93
 
 
94
 
 
95
        /*
 
96
         * Closes this input stream and releases any system resources associated
 
97
         * with the stream.
 
98
         *
 
99
         * <p> The <code>close</code> method of <code>InputStream</code> does
 
100
         * nothing.
 
101
         *
 
102
         * @exception  IOException      if an I/O error occurs.
 
103
         */
 
104
        public void close() throws IOException
 
105
        {
 
106
                try
 
107
                {
 
108
                        lo.close();
 
109
                        lo = null;
 
110
                }
 
111
                catch (SQLException se)
 
112
                {
 
113
                        throw new IOException(se.toString());
 
114
                }
 
115
        }
 
116
 
 
117
        /*
 
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.
 
121
         *
 
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
 
124
         * invalidated.
 
125
         *
 
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.
 
133
         *
 
134
         * <p> The <code>mark</code> method of <code>InputStream</code> does
 
135
         * nothing.
 
136
         *
 
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()
 
140
         */
 
141
        public synchronized void mark(int readlimit)
 
142
        {
 
143
                try
 
144
                {
 
145
                        mpos = lo.tell();
 
146
                }
 
147
                catch (SQLException se)
 
148
                {
 
149
                        //throw new IOException(se.toString());
 
150
                }
 
151
        }
 
152
 
 
153
        /*
 
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
 
159
         */
 
160
        public synchronized void reset()
 
161
        throws IOException
 
162
        {
 
163
                try
 
164
                {
 
165
                        lo.seek(mpos);
 
166
                }
 
167
                catch (SQLException se)
 
168
                {
 
169
                        throw new IOException(se.toString());
 
170
                }
 
171
        }
 
172
 
 
173
        /*
 
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>.
 
177
         *
 
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()
 
182
         */
 
183
        public boolean markSupported()
 
184
        {
 
185
                return true;
 
186
        }
 
187
}