Code example for RandomAccessFile

Methods: getChannel, readShort, seek

0
      // be applicable here. 
      throw new IllegalAccessError("This class should not instantiated.");
    } 
 
    static short readPositiveShort(RandomAccessFile file, long offset) throws IOException {
      file.seek(offset);
      short result = Short.reverseBytes(file.readShort());
      if (result < 0) {
        throw new IOException(
            "Unexpected negative value ; offset = " + offset + ", value = " + result);
      } 
      return result;
    } 
 
    static int readPositiveInt(RandomAccessFile file, long offset) throws IOException {
      int result = readInt(file, offset);
      if (result < 0) {
        throw new IOException(
            "Unexpected negative value ; offset = " + offset + ", value = " + result);
      } 
      return result;
    } 
 
    static int readInt(RandomAccessFile file, long offset) throws IOException {
      file.seek(offset);
      return Integer.reverseBytes(file.readInt());
    } 
 
    static void readBytes(RandomAccessFile file, long offset, byte[] result, int length)
        throws IOException { 
      file.seek(offset);
      file.read(result, 0, length);
    } 
  } 
 
  /** 
   * See http://www.pkware.com/documents/casestudies/APPNOTE.TXT 
   * I.  End of central directory record 
   */ 
  private static class EndOfCentralDirectory { 
    private final RandomAccessFile file;
    EndOfCentralDirectory(RandomAccessFile file) throws IOException {
      this.file = file;
      // Check the signature. 
      if (RandomAccessFileUtility.readInt(file, file.length() - ENDHDR) != ENDSIG) {
        throw new IOException("Invalid ENDHDR signature at " + (file.length() - ENDHDR));
      } 
    } 
 
    CentralDirectory getCentralDirectory() throws IOException { 
      // NOTE : Expects that there is no zip-file-comment at the tail of the file 
      //        becase aapt tool does not create such file which has "zip-file-comment" entry. 
      int centralDirectoryOffset =
          RandomAccessFileUtility.readPositiveInt(file, file.length() - ENDHDR + ENDOFF);
      return new CentralDirectory(file, centralDirectoryOffset);
    } 
  } 
 
  /** 
   * See http://www.pkware.com/documents/casestudies/APPNOTE.TXT 
   * F.  Central directory structure 
   */ 
  private static class CentralDirectory { 
    private final RandomAccessFile file;
    private final int centralDirectoryOffset;
    // The length is intentionally 256 (not 2^15) in order to prevent from creating large array. 
    private static final int MAX_FILE_NAME_LENGTH = 256;
    CentralDirectory(RandomAccessFile file, int offset) {
      this.file = file;
      this.centralDirectoryOffset = offset;
    } 
 
    LocalDirectory getLocalDirectory(String fileName) throws IOException {
      long offset = this.centralDirectoryOffset;
      byte[] temp = new byte[MAX_FILE_NAME_LENGTH];
      while (true) { 
        // Check the signature. 
        if (RandomAccessFileUtility.readInt(file, offset) != CENSIG) {
          throw new IOException("Invalid CENSIG signature at " + offset);
        } 
        int fileNameLength = RandomAccessFileUtility.readPositiveShort(file, offset + CENNAM);
        if (fileNameLength > MAX_FILE_NAME_LENGTH) {
          throw new IOException("Too long file name length ;" + fileNameLength);
        } 
        RandomAccessFileUtility.readBytes(file, offset + CENHDR, temp, fileNameLength);
        if (new String(temp, 0, fileNameLength, "UTF-8").equals(fileName)) {
          int localOffset = RandomAccessFileUtility.readPositiveInt(file, offset + CENOFF);
          int rawLength = RandomAccessFileUtility.readPositiveInt(file, offset + CENSIZ);
          return new LocalDirectory(file, localOffset, rawLength);
        } else { 
          int extLength = RandomAccessFileUtility.readPositiveShort(file, offset + CENEXT);
          int commentLength = RandomAccessFileUtility.readPositiveShort(file, offset + CENCOM);
          offset += CENHDR + fileNameLength + extLength + commentLength;
        } 
      } 
    } 
  } 
 
  /** 
   * See http://www.pkware.com/documents/casestudies/APPNOTE.TXT 
   * A.  Local file header 
   */ 
  private static class LocalDirectory { 
    private final RandomAccessFile file;
    private final long offset;
    private final int size;
 
    LocalDirectory(RandomAccessFile file, long offset, int size) throws IOException {
      this.file = file;
      this.offset = offset;
      this.size = size;
      // Check the signature. 
      if (RandomAccessFileUtility.readInt(file, offset) != LOCSIG) {
        throw new IOException("Invalid LOCSIG signature at " + offset);
      } 
    } 
 
    MappedByteBuffer getReadOnlyMappedByteBuffer() throws IOException {
      int fileNameLength = RandomAccessFileUtility.readPositiveShort(file, offset + LOCNAM);
      int extLength = RandomAccessFileUtility.readPositiveShort(file, offset + LOCEXT);
      FileChannel channel = file.getChannel();
      boolean succeeded = false;
      try { 
        MappedByteBuffer result =
            channel.map(MapMode.READ_ONLY,
                        offset + LOCHDR + fileNameLength + extLength, size);