PfdHelper.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package me.ag2s.base;
  2. import static me.ag2s.base.ThrowableUtils.rethrowAsIOException;
  3. import android.os.ParcelFileDescriptor;
  4. import android.system.ErrnoException;
  5. import android.system.OsConstants;
  6. import java.io.EOFException;
  7. import java.io.IOException;
  8. /**
  9. * 读取ParcelFileDescriptor的工具类
  10. */
  11. @SuppressWarnings("unused")
  12. public final class PfdHelper {
  13. /**
  14. * 读取基本类型的buffer
  15. */
  16. private static final byte[] readBuffer = new byte[8];
  17. public static void seek(ParcelFileDescriptor pfd, long pos) throws IOException {
  18. try {
  19. android.system.Os.lseek(pfd.getFileDescriptor(), pos, OsConstants.SEEK_SET);
  20. } catch (ErrnoException e) {
  21. throw rethrowAsIOException(e);
  22. }
  23. }
  24. public static long getFilePointer(ParcelFileDescriptor pfd) throws IOException {
  25. try {
  26. return android.system.Os.lseek(pfd.getFileDescriptor(), 0, OsConstants.SEEK_CUR);
  27. } catch (ErrnoException e) {
  28. throw rethrowAsIOException(e);
  29. }
  30. }
  31. public static long length(ParcelFileDescriptor pfd) throws IOException {
  32. try {
  33. return android.system.Os.fstat(pfd.getFileDescriptor()).st_size; //android.system.Os.lseek(pfd.getFileDescriptor(), 0, OsConstants.SEEK_END);
  34. } catch (ErrnoException e) {
  35. throw rethrowAsIOException(e);
  36. }
  37. }
  38. private static int readBytes(ParcelFileDescriptor pfd, byte[] b, int off, int len) throws IOException {
  39. try {
  40. return android.system.Os.read(pfd.getFileDescriptor(), b, off, len);
  41. } catch (ErrnoException e) {
  42. throw rethrowAsIOException(e);
  43. }
  44. }
  45. public static int read(ParcelFileDescriptor pfd) throws IOException {
  46. return (read(pfd, readBuffer, 0, 1) != -1) ? readBuffer[0] & 0xff : -1;
  47. }
  48. public static int read(ParcelFileDescriptor pfd, byte[] b, int off, int len) throws IOException {
  49. return readBytes(pfd, b, off, len);
  50. }
  51. public static int read(ParcelFileDescriptor pfd, byte[] b) throws IOException {
  52. return readBytes(pfd, b, 0, b.length);
  53. }
  54. public static void readFully(ParcelFileDescriptor pfd, byte[] b) throws IOException {
  55. readFully(pfd, b, 0, b.length);
  56. }
  57. public static void readFully(ParcelFileDescriptor pfd, byte[] b, int off, int len) throws IOException {
  58. int n = 0;
  59. do {
  60. int count = read(pfd, b, off + n, len - n);
  61. if (count < 0)
  62. throw new EOFException();
  63. n += count;
  64. } while (n < len);
  65. }
  66. public static int skipBytes(ParcelFileDescriptor pfd, int n) throws IOException {
  67. long pos;
  68. long len;
  69. long newpos;
  70. if (n <= 0) {
  71. return 0;
  72. }
  73. pos = getFilePointer(pfd);
  74. len = length(pfd);
  75. newpos = pos + n;
  76. if (newpos > len) {
  77. newpos = len;
  78. }
  79. seek(pfd, newpos);
  80. /* return the actual number of bytes skipped */
  81. return (int) (newpos - pos);
  82. }
  83. }