pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / ChecksumIndexInput.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.zip.CRC32;
22 import java.util.zip.Checksum;
23
24 /** Writes bytes through to a primary IndexOutput, computing
25  *  checksum as it goes. Note that you cannot use seek().
26  *
27  * @lucene.internal
28  */
29 public class ChecksumIndexInput extends IndexInput {
30   IndexInput main;
31   Checksum digest;
32
33   public ChecksumIndexInput(IndexInput main) {
34     super("ChecksumIndexInput(" + main + ")");
35     this.main = main;
36     digest = new CRC32();
37   }
38
39   @Override
40   public byte readByte() throws IOException {
41     final byte b = main.readByte();
42     digest.update(b);
43     return b;
44   }
45
46   @Override
47   public void readBytes(byte[] b, int offset, int len)
48     throws IOException {
49     main.readBytes(b, offset, len);
50     digest.update(b, offset, len);
51   }
52
53   
54   public long getChecksum() {
55     return digest.getValue();
56   }
57
58   @Override
59   public void close() throws IOException {
60     main.close();
61   }
62
63   @Override
64   public long getFilePointer() {
65     return main.getFilePointer();
66   }
67
68   @Override
69   public void seek(long pos) {
70     throw new RuntimeException("not allowed");
71   }
72
73   @Override
74   public long length() {
75     return main.length();
76   }
77 }