KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
36
37 class ClobWriter extends Writer JavaDoc {
38   private Store _store;
39
40   private StoreTransaction _xa;
41   
42   private TempBuffer _tempBuffer;
43   private byte []_buffer;
44   private int _offset;
45   private int _bufferEnd;
46
47   private long _length;
48
49   private Inode _inode;
50
51   private byte []_inodeBuffer;
52   private int _inodeOffset;
53
54   /**
55    * Creates a blob output stream.
56    *
57    * @param store the output store
58    */

59   ClobWriter(StoreTransaction xa, Store store, byte []inode, int inodeOffset)
60   {
61     init(xa, store, inode, inodeOffset);
62   }
63   
64   /**
65    * Creates a blob output stream.
66    *
67    * @param store the output store
68    */

69   ClobWriter(Inode inode)
70   {
71     init(null, inode.getStore(), inode.getBuffer(), 0);
72
73     _inode = inode;
74   }
75
76   /**
77    * Initialize the output stream.
78    */

79   public void init(StoreTransaction xa, Store store,
80            byte []inode, int inodeOffset)
81   {
82     if (xa == null)
83       xa = RawTransaction.create();
84       
85     _xa = xa;
86     
87     _store = store;
88
89     _length = 0;
90
91     _inodeBuffer = inode;
92     _inodeOffset = inodeOffset;
93
94     Inode.clear(_inodeBuffer, _inodeOffset);
95
96     _offset = 0;
97     
98     _tempBuffer = TempBuffer.allocate();
99     _buffer = _tempBuffer.getBuffer();
100     _bufferEnd = _buffer.length;
101   }
102
103   /**
104    * Writes a byte.
105    */

106   public void write(int v)
107     throws IOException JavaDoc
108   {
109     if (_bufferEnd <= _offset) {
110       flushBlock();
111     }
112
113     _buffer[_offset++] = (byte) (v >> 8);
114     _buffer[_offset++] = (byte) (v);
115     
116     _length += 2;
117   }
118
119   /**
120    * Writes a buffer.
121    */

122   public void write(char []buffer, int offset, int length)
123     throws IOException JavaDoc
124   {
125     byte []byteBuffer = _buffer;
126     int byteOffset = _offset;
127     
128     while (length > 0) {
129       if (_bufferEnd <= byteOffset) {
130     _offset = byteOffset;
131     flushBlock();
132     byteOffset = _offset;
133       }
134
135       int sublen = (_bufferEnd - byteOffset) >> 1;
136       if (length < sublen)
137     sublen = length;
138
139       for (int i = 0; i < sublen; i++) {
140     char ch = buffer[offset + i];
141     
142     byteBuffer[byteOffset++] = (byte) (ch >> 8);
143     byteBuffer[byteOffset++] = (byte) (ch);
144       }
145
146       offset += sublen;
147
148       length -= sublen;
149       _length += 2 * sublen;
150     }
151
152     _offset = byteOffset;
153   }
154
155   /**
156    * Updates the buffer.
157    */

158   private void flushBlock()
159     throws IOException JavaDoc
160   {
161     int length = _offset;
162     _offset = 0;
163
164     Inode.append(_inodeBuffer, _inodeOffset, _store, _xa, _buffer, 0, length);
165   }
166
167   /**
168    * Flushes the stream.
169    */

170   public void flush()
171   {
172   }
173
174   /**
175    * Completes the stream.
176    */

177   public void close()
178     throws IOException JavaDoc
179   {
180     try {
181       flushBlock();
182
183       TempBuffer.free(_tempBuffer);
184     } finally {
185       if (_inode != null)
186     _inode.closeOutputStream();
187     }
188   }
189
190   /**
191    * Writes the long.
192    */

193   private static void writeLong(byte []buffer, int offset, long v)
194   {
195     buffer[offset + 0] = (byte) (v >> 56);
196     buffer[offset + 1] = (byte) (v >> 48);
197     buffer[offset + 2] = (byte) (v >> 40);
198     buffer[offset + 3] = (byte) (v >> 32);
199     
200     buffer[offset + 4] = (byte) (v >> 24);
201     buffer[offset + 5] = (byte) (v >> 16);
202     buffer[offset + 6] = (byte) (v >> 8);
203     buffer[offset + 7] = (byte) (v);
204   }
205
206   /**
207    * Reads a long.
208    */

209   private static long readLong(byte []buffer, int offset)
210   {
211     return (((buffer[offset + 0] & 0xffL) << 56) |
212         ((buffer[offset + 1] & 0xffL) << 48) |
213         ((buffer[offset + 2] & 0xffL) << 40) |
214         ((buffer[offset + 3] & 0xffL) << 32) |
215         
216         ((buffer[offset + 4] & 0xffL) << 24) |
217         ((buffer[offset + 4] & 0xffL) << 16) |
218         ((buffer[offset + 4] & 0xffL) << 8) |
219         ((buffer[offset + 4] & 0xffL)));
220   }
221
222   /**
223    * Writes the short.
224    */

225   private static void writeShort(byte []buffer, int offset, short v)
226   {
227     buffer[offset + 0] = (byte) (v >> 8);
228     buffer[offset + 1] = (byte) (v);
229   }
230
231   /**
232    * Reads a short.
233    */

234   private static int readShort(byte []buffer, int offset)
235   {
236     return (((buffer[offset + 0] & 0xff) << 8) |
237         ((buffer[offset + 1] & 0xff)));
238   }
239 }
240
Popular Tags