KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > store > BlobOutputStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.store;
31
32 import com.caucho.vfs.TempBuffer;
33
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 public class BlobOutputStream extends OutputStream JavaDoc {
39   private static final Logger JavaDoc log
40     = Logger.getLogger(BlobOutputStream.class.getName());
41
42   private StoreTransaction _xa;
43   private Store _store;
44   
45   private TempBuffer _tempBuffer;
46   private byte []_buffer;
47   private int _offset;
48   private int _bufferEnd;
49
50   private Inode _inode;
51
52   private byte []_inodeBuffer;
53   private int _inodeOffset;
54   
55   /**
56    * Creates a blob output stream.
57    *
58    * @param store the output store
59    */

60   public BlobOutputStream(Transaction xa, Store store,
61               byte []inode, int inodeOffset)
62   {
63     init(store, inode, inodeOffset);
64
65     _xa = xa;
66   }
67   
68   /**
69    * Creates a blob output stream.
70    *
71    * @param store the output store
72    */

73   public BlobOutputStream(Store store, byte []inode, int inodeOffset)
74   {
75     init(store, inode, inodeOffset);
76   }
77   
78   /**
79    * Creates a blob output stream.
80    *
81    * @param store the output store
82    */

83   BlobOutputStream(Inode inode)
84   {
85     init(inode.getStore(), inode.getBuffer(), 0);
86
87     _inode = inode;
88   }
89
90   /**
91    * Initialize the output stream.
92    */

93   public void init(Store store, byte []inode, int inodeOffset)
94   {
95     _store = store;
96     _xa = RawTransaction.create();
97
98     _inodeBuffer = inode;
99     _inodeOffset = inodeOffset;
100
101     Inode.clear(_inodeBuffer, _inodeOffset);
102
103     _offset = 0;
104     
105     _tempBuffer = TempBuffer.allocate();
106     _buffer = _tempBuffer.getBuffer();
107     _bufferEnd = _buffer.length;
108   }
109
110   /**
111    * Writes a byte.
112    */

113   public void write(int v)
114     throws IOException JavaDoc
115   {
116     if (_bufferEnd <= _offset) {
117       flushBlock();
118     }
119
120     _buffer[_offset++] = (byte) v;
121   }
122
123   /**
124    * Writes a buffer.
125    */

126   public void write(byte []buffer, int offset, int length)
127     throws IOException JavaDoc
128   {
129     while (length > 0) {
130       if (_bufferEnd <= _offset) {
131     flushBlock();
132       }
133
134       int sublen = _bufferEnd - _offset;
135       if (length < sublen)
136     sublen = length;
137
138       System.arraycopy(buffer, offset, _buffer, _offset, sublen);
139
140       offset += sublen;
141       _offset += sublen;
142
143       length -= sublen;
144     }
145   }
146
147   /**
148    * Completes the stream.
149    */

150   public void close()
151     throws IOException JavaDoc
152   {
153     try {
154       if (_tempBuffer == null)
155     return;
156       
157       flushBlock();
158     } finally {
159       Inode inode = _inode;
160       _inode = null;
161       
162       if (inode != null)
163     inode.closeOutputStream();
164
165       _inodeBuffer = null;
166
167       TempBuffer tempBuffer = _tempBuffer;
168       _tempBuffer = null;
169
170       if (tempBuffer != null)
171     TempBuffer.free(tempBuffer);
172     }
173   }
174
175   /**
176    * Updates the buffer.
177    */

178   private void flushBlock()
179     throws IOException JavaDoc
180   {
181     int length = _offset;
182     _offset = 0;
183     
184     Inode.append(_inodeBuffer, _inodeOffset,
185          _store, _xa,
186          _buffer, 0, length);
187   }
188 }
189
Popular Tags