001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.store;
031:
032: import java.io.IOException;
033: import java.io.Reader;
034:
035: public class ClobReader extends Reader {
036: private static final int INODE_DIRECT_BLOCKS = 14;
037:
038: private Store _store;
039:
040: private long _length;
041: private long _offset;
042:
043: private byte[] _inode;
044: private int _inodeOffset;
045:
046: private long _lastOffset;
047: private long _fragmentId;
048:
049: private char[] _buffer;
050:
051: /**
052: * Creates a clob reader.
053: *
054: * @param store the backing store
055: */
056: public ClobReader(Store store, byte[] inode, int inodeOffset) {
057: init(store, inode, inodeOffset);
058: }
059:
060: /**
061: * Creates a clob reader.
062: *
063: * @param store the backing store
064: */
065: public ClobReader(Inode inode) {
066: init(inode.getStore(), inode.getBuffer(), 0);
067: }
068:
069: /**
070: * Initialize the output stream.
071: */
072: public void init(Store store, byte[] inode, int inodeOffset) {
073: if (store == null)
074: throw new NullPointerException();
075:
076: _store = store;
077:
078: _inode = inode;
079: _inodeOffset = inodeOffset;
080:
081: _length = readLong(inode, inodeOffset);
082: _offset = 0;
083:
084: _fragmentId = 0;
085: _lastOffset = 0;
086: }
087:
088: /**
089: * Reads a char.
090: */
091: public int read() throws IOException {
092: if (_buffer == null)
093: _buffer = new char[1];
094:
095: int len = read(_buffer, 0, 1);
096:
097: if (len < 0)
098: return -1;
099: else
100: return _buffer[0];
101: }
102:
103: /**
104: * Reads a buffer.
105: */
106: public int read(char[] buf, int offset, int length)
107: throws IOException {
108: int sublen = Inode.read(_inode, _inodeOffset, _store, _offset,
109: buf, offset, length);
110:
111: if (sublen > 0)
112: _offset += 2 * sublen;
113:
114: return sublen;
115: }
116:
117: /**
118: * Closes the buffer.
119: */
120: public void close() {
121: }
122:
123: /**
124: * Writes the long.
125: */
126: public static long readLong(byte[] buffer, int offset) {
127: return (((buffer[offset + 0] & 0xffL) << 56)
128: + ((buffer[offset + 1] & 0xffL) << 48)
129: + ((buffer[offset + 2] & 0xffL) << 40)
130: + ((buffer[offset + 3] & 0xffL) << 32)
131: + ((buffer[offset + 4] & 0xffL) << 24)
132: + ((buffer[offset + 5] & 0xffL) << 16)
133: + ((buffer[offset + 6] & 0xffL) << 8) + ((buffer[offset + 7] & 0xffL)));
134: }
135:
136: /**
137: * Writes the short.
138: */
139: private static int readShort(byte[] buffer, int offset) {
140: return (((buffer[offset + 0] & 0xff) << 8) + ((buffer[offset + 1] & 0xff)));
141: }
142: }
|