Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Store / CheckSumIndexOutput.cs
1 /* 
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
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 using System;
19
20 using Checksum = SupportClass.Checksum;
21 using CRC32 = SupportClass.CRC32;
22
23 namespace Mono.Lucene.Net.Store
24 {
25         
26         /// <summary>Writes bytes through to a primary IndexOutput, computing
27         /// checksum.  Note that you cannot use seek().
28         /// </summary>
29         public class ChecksumIndexOutput:IndexOutput
30         {
31                 internal IndexOutput main;
32                 internal Checksum digest;
33                 
34                 public ChecksumIndexOutput(IndexOutput main)
35                 {
36                         this.main = main;
37                         digest = new CRC32();
38                 }
39                 
40                 public override void  WriteByte(byte b)
41                 {
42                         digest.Update(b);
43                         main.WriteByte(b);
44                 }
45                 
46                 public override void  WriteBytes(byte[] b, int offset, int length)
47                 {
48                         digest.Update(b, offset, length);
49                         main.WriteBytes(b, offset, length);
50                 }
51                 
52                 public virtual long GetChecksum()
53                 {
54                         return digest.GetValue();
55                 }
56                 
57                 public override void  Flush()
58                 {
59                         main.Flush();
60                 }
61                 
62                 public override void  Close()
63                 {
64                         main.Close();
65                 }
66                 
67                 public override long GetFilePointer()
68                 {
69                         return main.GetFilePointer();
70                 }
71                 
72                 public override void  Seek(long pos)
73                 {
74                         throw new System.SystemException("not allowed");
75                 }
76                 
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.
81                 /// </summary>
82                 public virtual void  PrepareCommit()
83                 {
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);
92                         main.Flush();
93                         main.Seek(pos);
94                 }
95                 
96                 /// <summary>See {@link #prepareCommit} </summary>
97                 public virtual void  FinishCommit()
98                 {
99                         main.WriteLong(GetChecksum());
100                 }
101                 
102                 public override long Length()
103                 {
104                         return main.Length();
105                 }
106         }
107 }