2 * Copyright 2005 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 namespace Lucene
.Net
.Store
20 /// <summary>Base implementation class for buffered {@link IndexInput}. </summary>
21 public abstract class BufferedIndexInput
: IndexInput
, System
.ICloneable
23 internal static readonly int BUFFER_SIZE
;
25 private byte[] buffer
;
27 private long bufferStart
= 0; // position in file of buffer
28 private int bufferLength
= 0; // end of valid bytes
29 private int bufferPosition
= 0; // next byte to read
31 public override byte ReadByte()
33 if (bufferPosition
>= bufferLength
)
35 return buffer
[bufferPosition
++];
38 public override void ReadBytes(byte[] b
, int offset
, int len
)
40 if (len
< BUFFER_SIZE
)
42 for (int i
= 0; i
< len
; i
++)
44 b
[i
+ offset
] = (byte) ReadByte();
49 long start
= GetFilePointer();
51 ReadInternal(b
, offset
, len
);
53 bufferStart
= start
+ len
; // adjust stream variables
55 bufferLength
= 0; // trigger refill() on read
61 long start
= bufferStart
+ bufferPosition
;
62 long end
= start
+ BUFFER_SIZE
;
64 // don't read past EOF
66 bufferLength
= (int) (end
- start
);
67 if (bufferLength
<= 0)
68 throw new System
.IO
.IOException("read past EOF");
71 buffer
= new byte[BUFFER_SIZE
]; // allocate buffer lazily
72 ReadInternal(buffer
, 0, bufferLength
);
78 /// <summary>Expert: implements buffer refill. Reads bytes from the current position
81 /// <param name="b">the array to read bytes into
83 /// <param name="offset">the offset in the array to start storing bytes
85 /// <param name="length">the number of bytes to read
87 public abstract void ReadInternal(byte[] b
, int offset
, int length
);
89 public override long GetFilePointer()
91 return bufferStart
+ bufferPosition
;
94 public override void Seek(long pos
)
96 if (pos
>= bufferStart
&& pos
< (bufferStart
+ bufferLength
))
97 bufferPosition
= (int) (pos
- bufferStart
);
103 bufferLength
= 0; // trigger refill() on read()
108 /// <summary>Expert: implements seek. Sets current position in this file, where the
109 /// next {@link #ReadInternal(byte[],int,int)} will occur.
111 /// <seealso cref="#ReadInternal(byte[],int,int)">
113 public abstract void SeekInternal(long pos
);
115 public override System
.Object
Clone()
117 BufferedIndexInput clone
= (BufferedIndexInput
) base.Clone();
121 clone
.buffer
= new byte[BUFFER_SIZE
];
122 Array
.Copy(buffer
, 0, clone
.buffer
, 0, bufferLength
);
128 static BufferedIndexInput()
130 BUFFER_SIZE
= BufferedIndexOutput
.BUFFER_SIZE
;