/// <summary>(Optionally) seek to position, read and verify data.</summary> /// <remarks> /// (Optionally) seek to position, read and verify data. /// Seek to specified position if pos is non-negative. /// </remarks> /// <exception cref="System.IO.IOException"/> private void Pread(DFSInputStream @in, long pos, byte[] buffer, int offset, int length , byte[] authenticData) { NUnit.Framework.Assert.IsTrue("Test buffer too small", buffer.Length >= offset + length); if (pos >= 0) { @in.Seek(pos); } Log.Info("Reading from file of size " + @in.GetFileLength() + " at offset " + @in .GetPos()); while (length > 0) { int cnt = @in.Read(buffer, offset, length); NUnit.Framework.Assert.IsTrue("Error in read", cnt > 0); offset += cnt; length -= cnt; } // Verify for (int i = 0; i < length; ++i) { byte actual = buffer[i]; byte expect = authenticData[(int)pos + i]; NUnit.Framework.Assert.AreEqual("Read data mismatch at file offset " + (pos + i) + ". Expects " + expect + "; got " + actual, actual, expect); } }
/// <exception cref="System.IO.IOException"/> public virtual int Read(DFSInputStream dis, byte[] target, int startOff, int len) { int cnt = 0; lock (dis) { dis.Seek(startOff); while (cnt < len) { int read = dis.Read(target, cnt, len - cnt); if (read == -1) { return read; } cnt += read; } } return cnt; }
/// <exception cref="System.IO.IOException"/> public virtual int Read(DFSInputStream dis, byte[] target, int startOff, int len) { ByteBuffer bb = ByteBuffer.AllocateDirect(target.Length); int cnt = 0; lock (dis) { dis.Seek(startOff); while (cnt < len) { int read = dis.Read(bb); if (read == -1) { return read; } cnt += read; } } bb.Clear(); bb.Get(target); return cnt; }