/// <exception cref="System.Exception"/> public virtual void TestSeekAfterSetDropBehind() { // start a cluster Log.Info("testSeekAfterSetDropBehind"); Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; string TestPath = "/test"; int TestPathLen = MaxTestFileLen; try { cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); FileSystem fs = cluster.GetFileSystem(); CreateHdfsFile(fs, new Path(TestPath), TestPathLen, false); // verify that we can seek after setDropBehind FSDataInputStream fis = fs.Open(new Path(TestPath)); try { NUnit.Framework.Assert.IsTrue(fis.Read() != -1); // create BlockReader fis.SetDropBehind(false); // clear BlockReader fis.Seek(2); } finally { // seek fis.Close(); } } finally { if (cluster != null) { cluster.Shutdown(); } } }
/// <exception cref="System.Exception"/> internal static long ReadHdfsFile(FileSystem fs, Path p, long length, bool dropBehind ) { FSDataInputStream fis = null; long totalRead = 0; try { fis = fs.Open(p); if (dropBehind != null) { fis.SetDropBehind(dropBehind); } byte[] buf = new byte[8196]; while (length > 0) { int amt = (length > buf.Length) ? buf.Length : (int)length; int ret = fis.Read(buf, 0, amt); if (ret == -1) { return(totalRead); } totalRead += ret; length -= ret; } } catch (IOException e) { Log.Error("ioexception", e); } finally { if (fis != null) { fis.Close(); } } throw new RuntimeException("unreachable"); }