| 24 | import javax.annotation.Nullable; |
| 25 | |
| 26 | public class ElfZipFileChannel implements ElfByteChannel { |
| 27 | |
| 28 | private @Nullable InputStream mIs; |
| 29 | private final ZipEntry mZipEntry; |
| 30 | private final ZipFile mZipFile; |
| 31 | private final long mLength; |
| 32 | private boolean mOpened; |
| 33 | private long mPos; |
| 34 | |
| 35 | public ElfZipFileChannel(ZipFile zipFile, ZipEntry zipEntry) throws IOException { |
| 36 | mZipFile = zipFile; |
| 37 | mZipEntry = zipEntry; |
| 38 | |
| 39 | mOpened = true; |
| 40 | mPos = 0; |
| 41 | mLength = mZipEntry.getSize(); |
| 42 | mIs = mZipFile.getInputStream(mZipEntry); |
| 43 | if (mIs == null) { |
| 44 | throw new IOException(mZipEntry.getName() + "'s InputStream is null"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public long position() throws IOException { |
| 50 | return mPos; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public ElfByteChannel position(long newPosition) throws IOException { |
| 55 | if (mIs == null) { |
| 56 | throw new IOException(mZipEntry.getName() + "'s InputStream is null"); |
| 57 | } |
| 58 | |
| 59 | if (newPosition == mPos) { |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | if (newPosition > mLength) { |
| 64 | newPosition = mLength; |
| 65 | } |
| 66 | if (newPosition >= mPos) { |
| 67 | mIs.skip(newPosition - mPos); |
| 68 | } else { |
| 69 | mIs.close(); |
| 70 | mIs = mZipFile.getInputStream(mZipEntry); |
| 71 | if (mIs == null) { |
| 72 | throw new IOException(mZipEntry.getName() + "'s InputStream is null"); |
| 73 | } |
| 74 | mIs.skip(newPosition); |
| 75 | } |
| 76 | mPos = newPosition; |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Reads a sequence of bytes from this channel into the given buffer. Bytes are read starting at |
| 82 | * this channel's current file position, and then the file position is updated with the number of |
| 83 | * bytes actually read. |
nothing calls this directly
no outgoing calls
no test coverage detected