FileReader.java 991 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.oblivm.backend.gc.offline;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. public class FileReader {
  7. byte[] data;
  8. int pos = 0;
  9. public FileReader(String name) {
  10. try {
  11. File file = new File(name);
  12. FileInputStream fis;
  13. fis = new FileInputStream(file);
  14. data = new byte[(int) file.length()];
  15. fis.read(data);
  16. fis.close();
  17. } catch (IOException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. }
  22. public void read(byte[] a) {
  23. System.arraycopy(data, pos, a, 0, a.length);
  24. pos += a.length;
  25. }
  26. public byte[] read(int len) {
  27. byte[] res = Arrays.copyOfRange(data, pos, pos + len);
  28. pos += len;
  29. return res;
  30. }
  31. static public void main(String[] args) {
  32. double t1 = System.nanoTime();
  33. FileReader a = new FileReader("table");
  34. double t2 = System.nanoTime();
  35. System.out.println(t2 - t1);
  36. System.out.println(a.data.length);
  37. byte[] b = new byte[10];
  38. a.read(b);
  39. }
  40. }