KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
33 import java.io.Reader JavaDoc;
34
35 public class ClobReader extends Reader JavaDoc {
36   private static final int INODE_DIRECT_BLOCKS = 14;
37     
38   private Store _store;
39
40   private long _length;
41   private long _offset;
42
43   private byte []_inode;
44   private int _inodeOffset;
45
46   private long _lastOffset;
47   private long _fragmentId;
48
49   private char []_buffer;
50
51   /**
52    * Creates a clob reader.
53    *
54    * @param store the backing store
55    */

56   public ClobReader(Store store, byte []inode, int inodeOffset)
57   {
58     init(store, inode, inodeOffset);
59   }
60   
61   /**
62    * Creates a clob reader.
63    *
64    * @param store the backing store
65    */

66   public ClobReader(Inode inode)
67   {
68     init(inode.getStore(), inode.getBuffer(), 0);
69   }
70
71   /**
72    * Initialize the output stream.
73    */

74   public void init(Store store, byte []inode, int inodeOffset)
75   {
76     if (store == null)
77       throw new NullPointerException JavaDoc();
78     
79     _store = store;
80
81     _inode = inode;
82     _inodeOffset = inodeOffset;
83
84     _length = readLong(inode, inodeOffset);
85     _offset = 0;
86
87     _fragmentId = 0;
88     _lastOffset = 0;
89   }
90
91   /**
92    * Reads a char.
93    */

94   public int read()
95     throws IOException JavaDoc
96   {
97     if (_buffer == null)
98       _buffer = new char[1];
99
100     int len = read(_buffer, 0, 1);
101
102     if (len < 0)
103       return -1;
104     else
105       return _buffer[0];
106   }
107
108   /**
109    * Reads a buffer.
110    */

111   public int read(char []buf, int offset, int length)
112     throws IOException JavaDoc
113   {
114     int sublen = Inode.read(_inode, _inodeOffset,
115                 _store, _offset,
116                 buf, offset, length);
117
118     if (sublen > 0)
119       _offset += 2 * sublen;
120
121     return sublen;
122   }
123
124   /**
125    * Closes the buffer.
126    */

127   public void close()
128   {
129   }
130
131   /**
132    * Writes the long.
133    */

134   public static long readLong(byte []buffer, int offset)
135   {
136     return (((buffer[offset + 0] & 0xffL) << 56) +
137         ((buffer[offset + 1] & 0xffL) << 48) +
138         ((buffer[offset + 2] & 0xffL) << 40) +
139         ((buffer[offset + 3] & 0xffL) << 32) +
140         ((buffer[offset + 4] & 0xffL) << 24) +
141         ((buffer[offset + 5] & 0xffL) << 16) +
142         ((buffer[offset + 6] & 0xffL) << 8) +
143         ((buffer[offset + 7] & 0xffL)));
144   }
145
146   /**
147    * Writes the short.
148    */

149   private static int readShort(byte []buffer, int offset)
150   {
151     return (((buffer[offset + 0] & 0xff) << 8) +
152         ((buffer[offset + 1] & 0xff)));
153   }
154 }
155
Popular Tags