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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.store;
030:
031: import java.io.IOException;
032: import java.io.InputStream;
033:
034: /**
035: * Directly reading the blob from the inode.
036: */
037: public class InodeBlobInputStream extends InputStream {
038: private static final int INODE_DIRECT_BLOCKS = 14;
039:
040: private Store _store;
041:
042: private long _length;
043: private long _offset;
044:
045: private byte[] _inode;
046: private int _inodeOffset;
047:
048: private Block _block;
049: private byte[] _buffer;
050: private int _bufferOffset;
051: private int _bufferEnd;
052:
053: /**
054: * Creates a blob output stream.
055: *
056: * @param store the output store
057: */
058: public InodeBlobInputStream(Store store, byte[] inode,
059: int inodeOffset) {
060: init(store, inode, inodeOffset);
061: }
062:
063: /**
064: * Creates a blob output stream.
065: *
066: * @param store the output store
067: */
068: public InodeBlobInputStream(Inode inode) {
069: init(inode.getStore(), inode.getBuffer(), 0);
070: }
071:
072: /**
073: * Initialize the output stream.
074: */
075: public void init(Store store, byte[] inode, int inodeOffset) {
076: if (store == null)
077: throw new NullPointerException();
078:
079: _store = store;
080:
081: _inode = inode;
082: _inodeOffset = inodeOffset;
083:
084: _length = readLong(inode, inodeOffset);
085: _offset = 0;
086:
087: _block = null;
088:
089: if (_length <= Inode.INLINE_BLOB_SIZE) {
090: _buffer = inode;
091: _bufferOffset = inodeOffset + 8;
092: _bufferEnd = (int) (_bufferOffset + _length);
093: } else {
094: _buffer = null;
095: _bufferOffset = 0;
096: _bufferEnd = 0;
097: }
098: }
099:
100: /**
101: * Reads a byte.
102: */
103: public int read() throws IOException {
104: if (_length <= _offset)
105: return -1;
106:
107: if (_bufferEnd <= _bufferOffset)
108: readBlock();
109:
110: _offset++;
111:
112: return _buffer[_bufferOffset++] & 0xff;
113: }
114:
115: /**
116: * Reads a buffer.
117: */
118: public int read(byte[] buf, int offset, int length)
119: throws IOException {
120: if (_length <= _offset)
121: return -1;
122:
123: if (_bufferEnd <= _bufferOffset)
124: readBlock();
125:
126: int sublen = _bufferEnd - _bufferOffset;
127: if (length < sublen)
128: sublen = length;
129:
130: _offset += sublen;
131:
132: System.arraycopy(_buffer, _bufferOffset, buf, offset, sublen);
133:
134: _bufferOffset += sublen;
135:
136: return sublen;
137: }
138:
139: /**
140: * Closes the buffer.
141: */
142: public void close() {
143: if (_block != null) {
144: Block block = _block;
145: _block = null;
146: block.free();
147: }
148: }
149:
150: /**
151: * Updates the buffer.
152: */
153: public void readBlock() throws IOException {
154: if (_block != null) {
155: Block block = _block;
156: _block = null;
157: block.free();
158: }
159:
160: long addr;
161:
162: int blockCount = (int) (_offset / Store.BLOCK_SIZE);
163:
164: if (blockCount < INODE_DIRECT_BLOCKS) {
165: addr = readLong(_inode, _inodeOffset + 8 * (blockCount + 1));
166: } else {
167: long ptrAddr = readLong(_inode, _inodeOffset + 8
168: * (INODE_DIRECT_BLOCKS + 1));
169:
170: Block ptr = _store.readBlock(_store
171: .addressToBlockId(ptrAddr));
172:
173: addr = readLong(ptr.getBuffer(),
174: 8 * (blockCount - INODE_DIRECT_BLOCKS));
175:
176: ptr.free();
177: }
178:
179: _block = _store.readBlock(_store.addressToBlockId(addr));
180: _buffer = _block.getBuffer();
181:
182: int offset = (int) (addr & Store.BLOCK_OFFSET_MASK);
183:
184: if (offset > 0) {
185: _bufferOffset = readShort(_buffer, offset);
186: _bufferEnd = _bufferOffset + readShort(_buffer, offset + 2);
187: } else {
188: _bufferOffset = 0;
189: _bufferEnd = _buffer.length;
190: }
191: }
192:
193: /**
194: * Writes the long.
195: */
196: public static long readLong(byte[] buffer, int offset) {
197: return (((buffer[offset + 0] & 0xffL) << 56)
198: + ((buffer[offset + 1] & 0xffL) << 48)
199: + ((buffer[offset + 2] & 0xffL) << 40)
200: + ((buffer[offset + 3] & 0xffL) << 32)
201: + ((buffer[offset + 4] & 0xffL) << 24)
202: + ((buffer[offset + 5] & 0xffL) << 16)
203: + ((buffer[offset + 6] & 0xffL) << 8) + ((buffer[offset + 7] & 0xffL)));
204: }
205:
206: /**
207: * Writes the short.
208: */
209: private static int readShort(byte[] buffer, int offset) {
210: return (((buffer[offset + 0] & 0xff) << 8) + ((buffer[offset + 1] & 0xff)));
211: }
212: }
|