2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using Checksum = SupportClass.Checksum;
21 using CRC32 = SupportClass.CRC32;
23 namespace Mono.Lucene.Net.Store
26 /// <summary>Writes bytes through to a primary IndexOutput, computing
27 /// checksum. Note that you cannot use seek().
29 public class ChecksumIndexOutput:IndexOutput
31 internal IndexOutput main;
32 internal Checksum digest;
34 public ChecksumIndexOutput(IndexOutput main)
40 public override void WriteByte(byte b)
46 public override void WriteBytes(byte[] b, int offset, int length)
48 digest.Update(b, offset, length);
49 main.WriteBytes(b, offset, length);
52 public virtual long GetChecksum()
54 return digest.GetValue();
57 public override void Flush()
62 public override void Close()
67 public override long GetFilePointer()
69 return main.GetFilePointer();
72 public override void Seek(long pos)
74 throw new System.SystemException("not allowed");
77 /// <summary> Starts but does not complete the commit of this file (=
78 /// writing of the final checksum at the end). After this
79 /// is called must call {@link #finishCommit} and the
80 /// {@link #close} to complete the commit.
82 public virtual void PrepareCommit()
84 long checksum = GetChecksum();
85 // Intentionally write a mismatched checksum. This is
86 // because we want to 1) test, as best we can, that we
87 // are able to write a long to the file, but 2) not
88 // actually "commit" the file yet. This (prepare
89 // commit) is phase 1 of a two-phase commit.
90 long pos = main.GetFilePointer();
91 main.WriteLong(checksum - 1);
96 /// <summary>See {@link #prepareCommit} </summary>
97 public virtual void FinishCommit()
99 main.WriteLong(GetChecksum());
102 public override long Length()
104 return main.Length();