1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*-------------------------------------------------------------------------
*
* BlobOutputStream.java
* This implements a basic output stream that writes to a LargeObject
*
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/largeobject/Attic/BlobOutputStream.java,v 1.6 2003/03/07 18:39:45 barry Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.largeobject;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
public class BlobOutputStream extends OutputStream
{
/*
* The parent LargeObject
*/
private LargeObject lo;
/*
* Buffer
*/
private byte buf[];
/*
* Size of the buffer (default 1K)
*/
private int bsize;
/*
* Position within the buffer
*/
private int bpos;
/*
* Create an OutputStream to a large object
* @param lo LargeObject
*/
public BlobOutputStream(LargeObject lo)
{
this(lo, 1024);
}
/*
* Create an OutputStream to a large object
* @param lo LargeObject
* @param bsize The size of the buffer used to improve performance
*/
public BlobOutputStream(LargeObject lo, int bsize)
{
this.lo = lo;
this.bsize = bsize;
buf = new byte[bsize];
bpos = 0;
}
public void write(int b) throws java.io.IOException
{
try
{
if (bpos >= bsize)
{
lo.write(buf);
bpos = 0;
}
buf[bpos++] = (byte)b;
}
catch (SQLException se)
{
throw new IOException(se.toString());
}
}
public void write(byte[] buf, int off, int len) throws java.io.IOException
{
try
{
// If we have any internally buffered data, send it first
if ( bpos > 0 )
flush();
if ( off == 0 && len == buf.length )
lo.write(buf); // save a buffer creation and copy since full buffer written
else
lo.write(buf,off,len);
}
catch (SQLException se)
{
throw new IOException(se.toString());
}
}
/*
* Flushes this output stream and forces any buffered output bytes
* to be written out. The general contract of <code>flush</code> is
* that calling it is an indication that, if any bytes previously
* written have been buffered by the implementation of the output
* stream, such bytes should immediately be written to their
* intended destination.
*
* @exception IOException if an I/O error occurs.
*/
public void flush() throws IOException
{
try
{
if (bpos > 0)
lo.write(buf, 0, bpos);
bpos = 0;
}
catch (SQLException se)
{
throw new IOException(se.toString());
}
}
/*
* Closes this output stream and releases any system resources
* associated with this stream. The general contract of <code>close</code>
* is that it closes the output stream. A closed stream cannot perform
* output operations and cannot be reopened.
* <p>
* The <code>close</code> method of <code>OutputStream</code> does nothing.
*
* @exception IOException if an I/O error occurs.
*/
public void close() throws IOException
{
try
{
flush();
lo.close();
lo = null;
}
catch (SQLException se)
{
throw new IOException(se.toString());
}
}
}
|