add --shared
[pylucene.git] / lucene-java-3.4.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     this.main = main;
35     digest = new CRC32();
36   }
37
38   @Override
39   public byte readByte() throws IOException {
40     final byte b = main.readByte();
41     digest.update(b);
42     return b;
43   }
44
45   @Override
46   public void readBytes(byte[] b, int offset, int len)
47     throws IOException {
48     main.readBytes(b, offset, len);
49     digest.update(b, offset, len);
50   }
51
52   
53   public long getChecksum() {
54     return digest.getValue();
55   }
56
57   @Override
58   public void close() throws IOException {
59     main.close();
60   }
61
62   @Override
63   public long getFilePointer() {
64     return main.getFilePointer();
65   }
66
67   @Override
68   public void seek(long pos) {
69     throw new RuntimeException("not allowed");
70   }
71
72   @Override
73   public long length() {
74     return main.length();
75   }
76 }