~ubuntu-branches/ubuntu/oneiric/postgresql-pljava/oneiric

« back to all changes in this revision

Viewing changes to src/java/pljava/org/postgresql/pljava/jdbc/BlobValue.java

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut
  • Date: 2006-06-26 10:44:55 UTC
  • mfrom: (1.1.1 upstream) (3.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20060626104455-135i9wosat2k8vvt
Tags: 1.3.0-1
* New upstream release (closes: #375199)
* Built for postgresql 8.1 (closes: #339641)
* Rebuilt for new libgcj library (closes: #369986)
* Updated copyright file
* Updated standards version
* Made use of cdbs simple patchsys

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2004, 2005 TADA AB - Taby Sweden
 
2
 * Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
3
3
 * Distributed under the terms shown in the file COPYRIGHT
4
4
 * found in the root folder of this project or at
5
5
 * http://eng.tada.se/osprojects/COPYRIGHT.html
19
19
 */
20
20
public class BlobValue extends InputStream implements Blob
21
21
{
 
22
        public static int getStreamLength(InputStream value) throws SQLException
 
23
        {
 
24
                try
 
25
                {
 
26
                        value.mark(Integer.MAX_VALUE);
 
27
                        long length = value.skip(Long.MAX_VALUE);
 
28
                        if(length > Integer.MAX_VALUE)
 
29
                                throw new SQLException("stream content too large");
 
30
                        value.reset();
 
31
                        return (int)length;
 
32
                }
 
33
                catch(IOException e)
 
34
                {
 
35
                        throw new SQLException(e.getMessage());
 
36
                }
 
37
        }
 
38
 
 
39
        private long m_markPos;
 
40
        private final long m_nBytes;
22
41
        private final InputStream m_stream;
23
 
        private final long m_nBytes;
 
42
 
24
43
        private long m_streamPos;
25
 
        private long m_markPos;
26
44
 
27
45
        public BlobValue(byte[] bytes)
28
46
        {
29
47
                this(new ByteArrayInputStream(bytes), bytes.length);
30
48
        }
31
 
 
32
49
        public BlobValue(InputStream stream, long nBytes)
33
50
        {
34
51
                m_stream    = stream;
37
54
                m_markPos   = 0L;
38
55
        }
39
56
 
40
 
        //***************************************
41
 
        // Implementation of java.sql.Blob
42
 
        //***************************************
43
 
        public long length()
 
57
 
 
58
        //***************************************
 
59
        // Implementation of java.io.InputStream
 
60
        //***************************************
 
61
        public int available()
 
62
        throws IOException
44
63
        {
45
 
                return m_nBytes;
 
64
                return m_stream.available();
46
65
        }
47
66
 
48
67
        public InputStream getBinaryStream()
81
100
        }
82
101
 
83
102
        /**
84
 
         * Not supported.
85
 
         */
86
 
        public long position(byte[] pattern, long start)
87
 
        {
88
 
                throw new UnsupportedOperationException();
89
 
        }
90
 
 
91
 
        /**
92
 
         * Not supported.
93
 
         */
94
 
        public long position(Blob pattern, long start)
95
 
        {
96
 
                throw new UnsupportedOperationException();
97
 
        }
98
 
 
99
 
        //*************************************************************************
100
 
        // Implementation of java.sql.Blob JDK 1.4 methods
101
 
        //
102
 
        // Those method are intended to provide a channel to the underlying data
103
 
        // storage as an alternatvie to the setBinaryStream
104
 
        // on the preparedStatement and are not implemented by the BlobValue.
105
 
        //
106
 
        //*************************************************************************
107
 
        /**
108
 
         * In this method is not supported by <code>BlobValue</code>
109
 
         */
110
 
        public OutputStream setBinaryStream(long pos)
111
 
        {
112
 
                throw new UnsupportedOperationException();
113
 
        }
114
 
 
115
 
        /**
116
 
         * In this method is not supported by <code>BlobValue</code>
117
 
         */
118
 
        public int setBytes(long pos, byte[] bytes)
119
 
        {
120
 
                throw new UnsupportedOperationException();
121
 
        }
122
 
 
123
 
        /**
124
 
         * In this method is not supported by <code>BlobValue</code>
125
 
         */
126
 
        public int setBytes(long pos, byte[] bytes, int offset, int len)
127
 
        {
128
 
                throw new UnsupportedOperationException();
129
 
        }
130
 
 
131
 
        /**
132
 
         * In this method is not supported by <code>BlobValue</code>
133
 
         */
134
 
        public void truncate(long len)
135
 
        {
136
 
                throw new UnsupportedOperationException();
137
 
        }
138
 
 
139
 
        //***************************************
140
 
        // Implementation of java.io.InputStream
141
 
        //***************************************
142
 
        public int available()
143
 
        throws IOException
144
 
        {
145
 
                return m_stream.available();
146
 
        }
147
 
 
148
 
        public synchronized void mark(int readLimit)
149
 
        {
150
 
                m_stream.mark(readLimit);
151
 
                m_markPos = m_streamPos;
152
 
        }
153
 
 
154
 
        public boolean markSupported()
155
 
        {
156
 
                return m_stream.markSupported();
157
 
        }
158
 
 
159
 
        public synchronized int read()
160
 
        throws IOException
161
 
        {
162
 
                int rs = m_stream.read();
163
 
                m_streamPos++;
164
 
                return rs;
165
 
        }
166
 
 
167
 
        public synchronized int read(byte[] b)
168
 
        throws IOException
169
 
        {
170
 
                int rs = m_stream.read(b);
171
 
                m_streamPos += rs;
172
 
                return rs;
173
 
        }
174
 
 
175
 
        /**
176
103
         * Called from within...
177
104
         * @param buf a buffer that reflects the internally allocated bytea buffer.
178
105
         * This size of this buffer will be exactly the size returned by a call to
203
130
                m_streamPos += rs;
204
131
        }
205
132
 
 
133
        //***************************************
 
134
        // Implementation of java.sql.Blob
 
135
        //***************************************
 
136
        public long length()
 
137
        {
 
138
                return m_nBytes;
 
139
        }
 
140
 
 
141
        public synchronized void mark(int readLimit)
 
142
        {
 
143
                m_stream.mark(readLimit);
 
144
                m_markPos = m_streamPos;
 
145
        }
 
146
 
 
147
        public boolean markSupported()
 
148
        {
 
149
                return m_stream.markSupported();
 
150
        }
 
151
 
 
152
        /**
 
153
         * Not supported.
 
154
         */
 
155
        public long position(Blob pattern, long start)
 
156
        {
 
157
                throw new UnsupportedOperationException();
 
158
        }
 
159
 
 
160
        /**
 
161
         * Not supported.
 
162
         */
 
163
        public long position(byte[] pattern, long start)
 
164
        {
 
165
                throw new UnsupportedOperationException();
 
166
        }
 
167
 
 
168
        public synchronized int read()
 
169
        throws IOException
 
170
        {
 
171
                int rs = m_stream.read();
 
172
                m_streamPos++;
 
173
                return rs;
 
174
        }
 
175
 
 
176
        public synchronized int read(byte[] b)
 
177
        throws IOException
 
178
        {
 
179
                int rs = m_stream.read(b);
 
180
                m_streamPos += rs;
 
181
                return rs;
 
182
        }
 
183
 
206
184
        public synchronized int read(byte[] b, int off,  int len)
207
185
        throws IOException
208
186
        {
211
189
                return rs;
212
190
        }
213
191
 
 
192
        public synchronized void reset()
 
193
        throws IOException
 
194
        {
 
195
                m_stream.reset();
 
196
                m_streamPos = m_markPos;
 
197
        }
 
198
 
 
199
        //*************************************************************************
 
200
        // Implementation of java.sql.Blob JDK 1.4 methods
 
201
        //
 
202
        // Those method are intended to provide a channel to the underlying data
 
203
        // storage as an alternatvie to the setBinaryStream
 
204
        // on the preparedStatement and are not implemented by the BlobValue.
 
205
        //
 
206
        //*************************************************************************
 
207
        /**
 
208
         * In this method is not supported by <code>BlobValue</code>
 
209
         */
 
210
        public OutputStream setBinaryStream(long pos)
 
211
        {
 
212
                throw new UnsupportedOperationException();
 
213
        }
 
214
 
 
215
        /**
 
216
         * In this method is not supported by <code>BlobValue</code>
 
217
         */
 
218
        public int setBytes(long pos, byte[] bytes)
 
219
        {
 
220
                throw new UnsupportedOperationException();
 
221
        }
 
222
 
 
223
        /**
 
224
         * In this method is not supported by <code>BlobValue</code>
 
225
         */
 
226
        public int setBytes(long pos, byte[] bytes, int offset, int len)
 
227
        {
 
228
                throw new UnsupportedOperationException();
 
229
        }
 
230
 
214
231
        public synchronized long skip(long nBytes)
215
232
        throws IOException
216
233
        {
219
236
                return skipped;
220
237
        }
221
238
 
222
 
        public synchronized void reset()
223
 
        throws IOException
 
239
        /**
 
240
         * In this method is not supported by <code>BlobValue</code>
 
241
         */
 
242
        public void truncate(long len)
224
243
        {
225
 
                m_stream.reset();
226
 
                m_streamPos = m_markPos;
 
244
                throw new UnsupportedOperationException();
227
245
        }
228
246
}