Java 1.4:数组字节结束时,RandomAccesFile返回空值

时间:2018-10-23 01:22:31

标签: java

该类是多用户应用程序的一部分,用于查看文档,但是,当文件大小超过20 MB时,该类将不起作用。

问题在这里:

raf = new RandomAccessFile(this._file, "rw");
byte[] b = new byte[128000]; 

我也曾尝试将其更改为byte[] b = new byte[(int)raf];,但没有用。

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;

class PersistentByteServerV2$Runner extends Thread {
 private ReaderWriterLock _rwl;
 private File _file;
 private InputStream _is;
 private long _bytesRead;
 private boolean _locksAcquired = true;

 public PersistentByteServerV2$Runner(ReaderWriterLock rwl, File file,
  InputStream is) {
  this._rwl = rwl;

  try {
   this._rwl.startWrite();
   this._rwl.startRead();
  } catch (InterruptedException var5) {
   this._locksAcquired = false;
  }

  this._file = file;
  this._is = is;
  this._bytesRead = 0 L;
 }

 public void run() {
  RandomAccessFile raf = null;
  if (this._locksAcquired) {
   try {
    raf = new RandomAccessFile(this._file, "rw");
    byte[] b = new byte[128000]; // the value is set to this, as changing it to raf length does not work.
    long len = 0 L;

    do {
     len = (long) this._is.read(b); //InputStream
     if (len <= 0 L) {
      break;
     }

     raf.write(b, 0, (int) len); //write to the random access file
     if (this._bytesRead == 0 L) {
      this._rwl.endRead();
     }

     this._bytesRead += len;
    } while (len > -1 L);
   } catch (Throwable var18) {;
   } finally {
    try {
     raf.close();
    } catch (Throwable var17) {;
    }

    try {
     this._is.close();
    } catch (Throwable var16) {;
    }

    if (this._rwl.isWriterDown()) {
     this._rwl.endWrite();
    }

    if (this._rwl.isReaderDown()) {
     this._rwl.endRead();
    }

   }

  }
 }

 public long getBytesRead() {
  try {
   this._rwl.startRead();
   this._rwl.endRead();
  } catch (InterruptedException var2) {
   return -1 L;
  }

  return this._bytesRead;
 }
}

0 个答案:

没有答案