001: package org.apache.lucene.store.je;
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: * Port of Andi Vajda's DbDirectory to Java Edition of Berkeley Database
025: *
026: * @author Aaron Donovan
027: */
028:
029: public class JEIndexInput extends IndexInput {
030:
031: protected long position = 0L, length = 0L;
032:
033: protected JEDirectory directory;
034:
035: protected Block block;
036:
037: protected File file;
038:
039: protected JEIndexInput(JEDirectory directory, String name)
040: throws IOException {
041: super ();
042:
043: this .directory = directory;
044:
045: this .file = new File(name);
046: if (!file.exists(directory))
047: throw new IOException("File does not exist: " + name);
048:
049: length = file.getLength();
050:
051: block = new Block(file);
052: block.get(directory);
053: }
054:
055: public Object clone() {
056: try {
057: JEIndexInput clone = (JEIndexInput) super .clone();
058:
059: clone.block = new Block(file);
060: clone.block.seek(position);
061: clone.block.get(directory);
062:
063: return clone;
064: } catch (IOException e) {
065: throw new RuntimeException(e.getMessage());
066: }
067: }
068:
069: public void close() throws IOException {
070: }
071:
072: public long length() {
073: return length;
074: }
075:
076: public byte readByte() throws IOException {
077: if (position + 1 > length)
078: throw new IOException(file.getName()
079: + ": Reading past end of file");
080:
081: int blockPos = (int) (position++ & JEIndexOutput.BLOCK_MASK);
082: byte b = block.getData()[blockPos];
083:
084: if (blockPos + 1 == JEIndexOutput.BLOCK_LEN) {
085: block.seek(position);
086: block.get(directory);
087: }
088:
089: return b;
090: }
091:
092: public void readBytes(byte[] b, int offset, int len)
093: throws IOException {
094: if (position + len > length)
095: throw new IOException("Reading past end of file");
096: else {
097: int blockPos = (int) (position & JEIndexOutput.BLOCK_MASK);
098:
099: while (blockPos + len >= JEIndexOutput.BLOCK_LEN) {
100: int blockLen = JEIndexOutput.BLOCK_LEN - blockPos;
101:
102: System.arraycopy(block.getData(), blockPos, b, offset,
103: blockLen);
104:
105: len -= blockLen;
106: offset += blockLen;
107: position += blockLen;
108:
109: block.seek(position);
110: block.get(directory);
111: blockPos = 0;
112: }
113:
114: if (len > 0) {
115: System.arraycopy(block.getData(), blockPos, b, offset,
116: len);
117: position += len;
118: }
119: }
120: }
121:
122: public void seek(long pos) throws IOException {
123: if (pos > length)
124: throw new IOException("seeking past end of file");
125:
126: if ((pos >>> JEIndexOutput.BLOCK_SHIFT) != (position >>> JEIndexOutput.BLOCK_SHIFT)) {
127: block.seek(pos);
128: block.get(directory);
129: }
130:
131: position = pos;
132: }
133:
134: public long getFilePointer() {
135: return position;
136: }
137: }
|