001: package org.apache.lucene.store.db;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import org.apache.lucene.store.IndexInput;
022:
023: /**
024: * @author Andi Vajda
025: */
026:
027: public class DbIndexInput extends IndexInput {
028:
029: protected long position = 0L, length = 0L;
030: protected DbDirectory directory;
031: protected Block block;
032: protected File file;
033:
034: protected DbIndexInput(DbDirectory directory, String name)
035: throws IOException {
036: super ();
037:
038: this .directory = directory;
039:
040: this .file = new File(name);
041: if (!file.exists(directory))
042: throw new IOException("File does not exist: " + name);
043:
044: length = file.getLength();
045:
046: block = new Block(file);
047: block.get(directory);
048: }
049:
050: public Object clone() {
051: try {
052: DbIndexInput clone = (DbIndexInput) super .clone();
053:
054: clone.block = new Block(file);
055: clone.block.seek(position);
056: clone.block.get(directory);
057:
058: return clone;
059: } catch (IOException e) {
060: throw new RuntimeException(e.getMessage());
061: }
062: }
063:
064: public void close() throws IOException {
065: }
066:
067: public long length() {
068: return length;
069: }
070:
071: public byte readByte() throws IOException {
072: if (position + 1 > length)
073: throw new IOException("Reading past end of file");
074:
075: int blockPos = (int) (position++ & DbIndexOutput.BLOCK_MASK);
076: byte b = block.getData()[blockPos];
077:
078: if (blockPos + 1 == DbIndexOutput.BLOCK_LEN) {
079: block.seek(position);
080: block.get(directory);
081: }
082:
083: return b;
084: }
085:
086: public void readBytes(byte[] b, int offset, int len)
087: throws IOException {
088: if (position + len > length)
089: throw new IOException("Reading past end of file");
090: else {
091: int blockPos = (int) (position & DbIndexOutput.BLOCK_MASK);
092:
093: while (blockPos + len >= DbIndexOutput.BLOCK_LEN) {
094: int blockLen = DbIndexOutput.BLOCK_LEN - blockPos;
095:
096: System.arraycopy(block.getData(), blockPos, b, offset,
097: blockLen);
098:
099: len -= blockLen;
100: offset += blockLen;
101: position += blockLen;
102:
103: block.seek(position);
104: block.get(directory);
105: blockPos = 0;
106: }
107:
108: if (len > 0) {
109: System.arraycopy(block.getData(), blockPos, b, offset,
110: len);
111: position += len;
112: }
113: }
114: }
115:
116: public void seek(long pos) throws IOException {
117: if (pos > length)
118: throw new IOException("seeking past end of file");
119:
120: if ((pos >>> DbIndexOutput.BLOCK_SHIFT) != (position >>> DbIndexOutput.BLOCK_SHIFT)) {
121: block.seek(pos);
122: block.get(directory);
123: }
124:
125: position = pos;
126: }
127:
128: public long getFilePointer() {
129: return position;
130: }
131: }
|