private void Refill() { long start = bufferStart + bufferPosition; long end = start + bufferSize; if (end > Length) // don't read past EOF { end = Length; } int newLength = (int)(end - start); if (newLength <= 0) { throw EOFException.Create("read past EOF: " + this); } if (m_buffer == null) { NewBuffer(new byte[bufferSize]); // allocate buffer lazily SeekInternal(bufferStart); } ReadInternal(m_buffer, 0, newLength); bufferLength = newLength; bufferStart = start; bufferPosition = 0; }
public override sealed void ReadBytes(byte[] b, int offset, int len) { // LUCENENET: Refactored to avoid calls on invalid conditions instead of // catching and re-throwing exceptions in the normal workflow. EnsureOpen(); int curAvail = curBuf.Remaining; if (len <= curAvail) { curBuf.Get(b, offset, len); } else { while (len > curAvail) { curBuf.Get(b, offset, curAvail); len -= curAvail; offset += curAvail; curBufIndex++; if (curBufIndex >= buffers.Length) { throw EOFException.Create("read past EOF: " + this); } curBuf = buffers[curBufIndex]; curBuf.Position = 0; curAvail = curBuf.Remaining; } curBuf.Get(b, offset, len); } }
private void SwitchCurrentBuffer(bool enforceEOF) { bufferStart = (long)BUFFER_SIZE * (long)currentBufferIndex; if (currentBufferIndex >= file.NumBuffers) { // end of file reached, no more buffers left if (enforceEOF) { throw EOFException.Create("read past EOF: " + this); } else { // Force EOF if a read takes place at this position currentBufferIndex--; bufferPosition = BUFFER_SIZE; } } else { currentBuffer = file.GetBuffer(currentBufferIndex); bufferPosition = 0; long buflen = length - bufferStart; bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int)buflen; } }
protected override void ReadInternal(byte[] b, int offset, int len) { ByteBuffer bb; // Determine the ByteBuffer we should use if (b == m_buffer && 0 == offset) { // Use our own pre-wrapped byteBuf: if (Debugging.AssertsEnabled) { Debugging.Assert(byteBuf != null); } byteBuf.Clear(); byteBuf.Limit = len; bb = byteBuf; } else { bb = ByteBuffer.Wrap(b, offset, len); } int readOffset = bb.Position; int readLength = bb.Limit - readOffset; long pos = Position + m_off; // LUCENENET specific: Renamed from getFilePointer() to match FileStream if (pos + len > m_end) { throw EOFException.Create("read past EOF: " + this); } try { while (readLength > 0) { int toRead = Math.Min(CHUNK_SIZE, readLength); bb.Limit = readOffset + toRead; if (Debugging.AssertsEnabled) { Debugging.Assert(bb.Remaining == toRead); } int i = m_channel.Read(bb, pos); if (i <= 0) // be defensive here, even though we checked before hand, something could have changed { throw EOFException.Create("read past EOF: " + this + " off: " + offset + " len: " + len + " pos: " + pos + " chunkLen: " + readLength + " end: " + m_end); } pos += i; readOffset += i; readLength -= i; } if (Debugging.AssertsEnabled) { Debugging.Assert(readLength == 0); } } catch (Exception ioe) when(ioe.IsIOException()) { throw new IOException(ioe.ToString() + ": " + this, ioe); } }
/// <summary> /// Skip exactly <paramref name="count"/> values. </summary> public void Skip(long count) { if (Debugging.AssertsEnabled) { Debugging.Assert(count >= 0); } if (ord + count > valueCount || ord + count < 0) { throw EOFException.Create(); } // 1. skip buffered values int skipBuffer = (int)Math.Min(count, blockSize - off); off += skipBuffer; ord += skipBuffer; count -= skipBuffer; if (count == 0L) { return; } // 2. skip as many blocks as necessary if (Debugging.AssertsEnabled) { Debugging.Assert(off == blockSize); } while (count >= blockSize) { int token = @in.ReadByte() & 0xFF; int bitsPerValue = token.TripleShift(AbstractBlockPackedWriter.BPV_SHIFT); if (bitsPerValue > 64) { throw new IOException("Corrupted"); } if ((token & AbstractBlockPackedWriter.MIN_VALUE_EQUALS_0) == 0) { ReadVInt64(@in); } long blockBytes = PackedInt32s.Format.PACKED.ByteCount(packedIntsVersion, blockSize, bitsPerValue); SkipBytes(blockBytes); ord += blockSize; count -= blockSize; } if (count == 0L) { return; } // 3. skip last values if (Debugging.AssertsEnabled) { Debugging.Assert(count < blockSize); } Refill(); ord += count; off += (int)count; }
public override byte ReadByte() { int v = _reader.ReadByte(); if (v == -1) { throw EOFException.Create(); } return((byte)v); }
public virtual void TestWrapEOFException() { IOException e = new EOFException("eof"); IOException wrapped = VerifyExceptionClass(e, typeof(EOFException)); AssertInException(wrapped, "eof"); AssertWikified(wrapped); AssertInException(wrapped, "localhost"); AssertRemoteDetailsIncluded(wrapped); AssertInException(wrapped, "/EOFException"); }
/// <summary> /// Expert: implements buffer refill. Reads bytes from the current /// position in the input. </summary> /// <param name="b"> the array to read bytes into </param> /// <param name="offset"> the offset in the array to start storing bytes </param> /// <param name="len"> the number of bytes to read </param> protected override void ReadInternal(byte[] b, int offset, int len) { long start = Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream if (start + len > length) { throw EOFException.Create("read past EOF: " + this); } @base.Seek(fileOffset + start); @base.ReadBytes(b, offset, len, false); }
internal virtual void FillBuffer() { if (Debugging.AssertsEnabled) Debugging.Assert(decompressed <= length); if (decompressed == length) { throw EOFException.Create(); } int toDecompress = Math.Min(length - decompressed, outerInstance.chunkSize); outerInstance.decompressor.Decompress(outerInstance.fieldsStream, toDecompress, 0, toDecompress, outerInstance.bytes); decompressed += toDecompress; }
/// <summary> /// Read the next value. </summary> public long Next() { if (ord == valueCount) { throw EOFException.Create(); } if (off == blockSize) { Refill(); } long value = values[off++]; ++ord; return value; }
public override void ReadBytes(byte[] b, int offset, int len) { while (len > 0) { int cnt = _reader.Read(b, offset, len); if (cnt < 0) { // Partially read the input, but no more data available in the stream. throw EOFException.Create(); } len -= cnt; offset += cnt; } }
/// <summary> /// <see cref="IndexInput"/> methods </summary> protected override void ReadInternal(byte[] b, int offset, int len) { UninterruptableMonitor.Enter(m_file); try { long position = m_off + Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream m_file.Seek(position, SeekOrigin.Begin); int total = 0; if (position + len > m_end) { throw EOFException.Create("read past EOF: " + this); } try { //while (total < len) //{ // int toRead = Math.Min(CHUNK_SIZE, len - total); // int i = m_file.Read(b, offset + total, toRead); // if (i < 0) // be defensive here, even though we checked before hand, something could have changed // { // throw EOFException.Create("read past EOF: " + this + " off: " + offset + " len: " + len + " total: " + total + " chunkLen: " + toRead + " end: " + m_end); // } // if (Debugging.AssertsEnabled) Debugging.Assert(i > 0, "RandomAccessFile.read with non zero-length toRead must always read at least one byte"); // total += i; //} // LUCENENET specific: FileStream is already optimized to read natively // using the buffer size that is passed through its constructor. So, // all we need to do is Read(). total = m_file.Read(b, offset, len); if (Debugging.AssertsEnabled) { Debugging.Assert(total == len); } } catch (Exception ioe) when(ioe.IsIOException()) { throw new IOException(ioe.Message + ": " + this, ioe); } } finally { UninterruptableMonitor.Exit(m_file); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldRequestIndexPopulationWhenFailingWithEOFException() internal virtual void ShouldRequestIndexPopulationWhenFailingWithEOFException() { // Given long faultyIndexId = 1; EOFException error = new EOFException("/some/path/somewhere"); LuceneIndexProvider provider = NewFaultyIndexProvider(faultyIndexId, error); // When StoreIndexDescriptor descriptor = forSchema(forLabel(1, 1), provider.ProviderDescriptor).withId(faultyIndexId); InternalIndexState initialState = provider.GetInitialState(descriptor); // Then assertThat(initialState, equalTo(InternalIndexState.POPULATING)); _logProvider.assertAtLeastOnce(LoggedException(error)); }
public override void Add(long v) { if (Debugging.AssertsEnabled) { Debugging.Assert(m_bitsPerValue == 64 || (v >= 0 && v <= PackedInt32s.MaxValue(m_bitsPerValue)), "{0}", m_bitsPerValue); Debugging.Assert(!finished); } if (m_valueCount != -1 && written >= m_valueCount) { throw EOFException.Create("Writing past end of stream"); } nextValues[off++] = v; if (off == nextValues.Length) { Flush(); } ++written; }
/// <summary> /// Read between <c>1</c> and <paramref name="count"/> values. </summary> public Int64sRef Next(int count) { if (Debugging.AssertsEnabled) Debugging.Assert(count > 0); if (ord == valueCount) { throw EOFException.Create(); } if (off == blockSize) { Refill(); } count = Math.Min(count, blockSize - off); count = (int)Math.Min(count, valueCount - ord); valuesRef.Offset = off; valuesRef.Length = count; off += count; ord += count; return valuesRef; }
public override sealed void Seek(long pos) { // LUCENENET: Refactored to avoid calls on invalid conditions instead of // catching and re-throwing exceptions in the normal workflow. EnsureOpen(); // necessary in case offset != 0 and pos < 0, but pos >= -offset if (pos < 0L) { throw new ArgumentOutOfRangeException("Seeking to negative position: " + this); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) } pos += offset; // we use >> here to preserve negative, so we will catch AIOOBE, // in case pos + offset overflows. int bi = (int)(pos >> chunkSizePower); // LUCENENET: Defensive programming so we don't get an IndexOutOfRangeException // when reading from buffers. if (bi < 0 || bi >= buffers.Length) { throw EOFException.Create("seek past EOF: " + this); } ByteBuffer b = buffers[bi]; int newPosition = (int)(pos & chunkSizeMask); // LUCENENET: Defensive programming so we don't get an ArgumentOutOfRangeException // when setting b.Position. if (newPosition < 0 || newPosition > b.Limit) { throw EOFException.Create("seek past EOF: " + this); } b.Position = newPosition; // write values, on exception all is unchanged this.curBufIndex = bi; this.curBuf = b; // LUCENENET: Already checked buffers to see if it was null in EnsureOpen(). // If we get a NullReferenceException we definitely need it to be thrown so it is known. }
public override sealed byte ReadByte() { // LUCENENET: Refactored to avoid calls on invalid conditions instead of // catching and re-throwing exceptions in the normal workflow. EnsureOpen(); if (curBuf.HasRemaining) { return(curBuf.Get()); } do { curBufIndex++; if (curBufIndex >= buffers.Length) { throw EOFException.Create("read past EOF: " + this); } curBuf = buffers[curBufIndex]; curBuf.Position = 0; } while (!curBuf.HasRemaining); return(curBuf.Get()); }
public override Int64sRef Next(int count) { if (Debugging.AssertsEnabled) { Debugging.Assert(nextValues.Length >= 0); Debugging.Assert(count > 0); Debugging.Assert(nextValues.Offset + nextValues.Length <= nextValues.Int64s.Length); } nextValues.Offset += nextValues.Length; int remaining = m_valueCount - position - 1; if (remaining <= 0) { throw EOFException.Create(); } count = Math.Min(remaining, count); if (nextValues.Offset == nextValues.Int64s.Length) { long remainingBlocks = format.ByteCount(packedIntsVersion, remaining, m_bitsPerValue); int blocksToRead = (int)Math.Min(remainingBlocks, nextBlocks.Length); m_in.ReadBytes(nextBlocks, 0, blocksToRead); if (blocksToRead < nextBlocks.Length) { Arrays.Fill(nextBlocks, blocksToRead, nextBlocks.Length, (byte)0); } bulkOperation.Decode(nextBlocks, 0, nextValues.Int64s, 0, iterations); nextValues.Offset = 0; } nextValues.Length = Math.Min(nextValues.Int64s.Length - nextValues.Offset, count); position += nextValues.Length; return(nextValues); }
protected BinaryDictionary() { int[] targetMapOffsets = null, targetMap = null; string[] posDict = null; string[] inflFormDict = null; string[] inflTypeDict = null; ByteBuffer buffer; // LUCENENET: IDE0059: Remove unnecessary value assignment using (Stream mapIS = GetResource(TARGETMAP_FILENAME_SUFFIX)) { DataInput @in = new InputStreamDataInput(mapIS); CodecUtil.CheckHeader(@in, TARGETMAP_HEADER, VERSION, VERSION); targetMap = new int[@in.ReadVInt32()]; targetMapOffsets = new int[@in.ReadVInt32()]; int accum = 0, sourceId = 0; for (int ofs = 0; ofs < targetMap.Length; ofs++) { int val = @in.ReadVInt32(); if ((val & 0x01) != 0) { targetMapOffsets[sourceId] = ofs; sourceId++; } accum += val.TripleShift(1); targetMap[ofs] = accum; } if (sourceId + 1 != targetMapOffsets.Length) { throw new IOException("targetMap file format broken"); } targetMapOffsets[sourceId] = targetMap.Length; } using (Stream posIS = GetResource(POSDICT_FILENAME_SUFFIX)) { DataInput @in = new InputStreamDataInput(posIS); CodecUtil.CheckHeader(@in, POSDICT_HEADER, VERSION, VERSION); int posSize = @in.ReadVInt32(); posDict = new string[posSize]; inflTypeDict = new string[posSize]; inflFormDict = new string[posSize]; for (int j = 0; j < posSize; j++) { posDict[j] = @in.ReadString(); inflTypeDict[j] = @in.ReadString(); inflFormDict[j] = @in.ReadString(); // this is how we encode null inflections if (inflTypeDict[j].Length == 0) { inflTypeDict[j] = null; } if (inflFormDict[j].Length == 0) { inflFormDict[j] = null; } } } ByteBuffer tmpBuffer; using (Stream dictIS = GetResource(DICT_FILENAME_SUFFIX)) { // no buffering here, as we load in one large buffer DataInput @in = new InputStreamDataInput(dictIS); CodecUtil.CheckHeader(@in, DICT_HEADER, VERSION, VERSION); int size = @in.ReadVInt32(); tmpBuffer = ByteBuffer.Allocate(size); // AllocateDirect..? int read = dictIS.Read(tmpBuffer.Array, 0, size); if (read != size) { throw EOFException.Create("Cannot read whole dictionary"); } } buffer = tmpBuffer.AsReadOnlyBuffer(); this.targetMap = targetMap; this.targetMapOffsets = targetMapOffsets; this.posDict = posDict; this.inflTypeDict = inflTypeDict; this.inflFormDict = inflFormDict; this.buffer = buffer; }
public override sealed void ReadBytes(byte[] b, int offset, int len, bool useBuffer) { int available = bufferLength - bufferPosition; if (len <= available) { // the buffer contains enough data to satisfy this request if (len > 0) // to allow b to be null if len is 0... { Buffer.BlockCopy(m_buffer, bufferPosition, b, offset, len); } bufferPosition += len; } else { // the buffer does not have enough data. First serve all we've got. if (available > 0) { Buffer.BlockCopy(m_buffer, bufferPosition, b, offset, available); offset += available; len -= available; bufferPosition += available; } // and now, read the remaining 'len' bytes: if (useBuffer && len < bufferSize) { // If the amount left to read is small enough, and // we are allowed to use our buffer, do it in the usual // buffered way: fill the buffer and copy from it: Refill(); if (bufferLength < len) { // Throw an exception when refill() could not read len bytes: Buffer.BlockCopy(m_buffer, 0, b, offset, bufferLength); throw EOFException.Create("read past EOF: " + this); } else { Buffer.BlockCopy(m_buffer, 0, b, offset, len); bufferPosition = len; } } else { // The amount left to read is larger than the buffer // or we've been asked to not use our buffer - // there's no performance reason not to read it all // at once. Note that unlike the previous code of // this function, there is no need to do a seek // here, because there's no need to reread what we // had in the buffer. long after = bufferStart + bufferPosition + len; if (after > Length) { throw EOFException.Create("read past EOF: " + this); } ReadInternal(b, offset, len); bufferStart = after; bufferPosition = 0; bufferLength = 0; // trigger refill() on read } } }