public static PartitionSystemType[] detectPartitionSystem(ReadableRandomAccessStream psStream) {
   long len;
   try {
     len = psStream.length();
   } catch (RuntimeIOException e) {
     len = -1;
   }
   return detectPartitionSystem(psStream, 0, len);
 }
  private static byte[] readData(ReadableRandomAccessStream llf, long offset) {
    byte[] data = null;
    int curBlockSize = 512;
    RuntimeIOException mostRecentException = null;

    do {
      data = new byte[curBlockSize];
      try {
        llf.seek(offset);
        llf.readFully(data);
      } catch (RuntimeIOException ex) {
        /* It's possible that an exception is thrown if the device
         * requires aligned access. In that case ignore and increase the
         * block size until we hit the required alignment block size. */

        mostRecentException = ex;
        data = null;
      }
    } while (data == null && (curBlockSize *= 2) <= 4096);

    if (data == null) {
      /* We got I/O errors when attempting to read using all attempted
       * block sizes. This must be something unrelated to alignment, and
       * we cannot continue so throw the most recent exception. */

      throw mostRecentException;
    }

    DriverDescriptorRecord ddrTmp = new DriverDescriptorRecord(data, 0);
    if (ddrTmp.isValid() && ddrTmp.getSbBlkSize() > data.length) {
      /* Logical block size is different than our current block size.
       * Re-read with logical block size to get entire driver descriptor
       * record. */

      data = new byte[ddrTmp.getSbBlkSize()];
      llf.seek(offset);
      llf.readFully(data);
    }

    return data;
  }
 public static PartitionSystemType[] detectPartitionSystem(DataLocator inputDataLocator) {
   ReadableRandomAccessStream dlStream = inputDataLocator.createReadOnlyFile();
   PartitionSystemType[] result = detectPartitionSystem(dlStream);
   dlStream.close();
   return result;
 }
Exemplo n.º 4
0
 /* @Override */
 public void close() {
   sourceFile.close();
 }
Exemplo n.º 5
0
  /* @Override */
  public int read(byte[] data, int pos, int len) {
    // System.err.println("ForkFilter.read(" + data + ", " + pos + ", " + len);
    long offset = Long.MAX_VALUE; // MAX_VALUE as a sentinel for seek
    long bytesToSkip = logicalPosition;
    long curLogicalBlock = 0;
    int extIndex;
    long currentExtentLength;

    if (extentDescriptors.size() < 1 || logicalPosition > forkLength) {
      return -1; // EOF
    }

    // Skip all extents whose range is located before the requested position (logicalPosition)
    // System.out.println("ForkFilter.read: skipping extents (bytesToSkip=" +
    //        bytesToSkip + ")...");
    for (extIndex = 0; ; ++extIndex) {
      CommonHFSExtentDescriptor cur = getExtent(extIndex, curLogicalBlock);
      if (cur == null) {
        /* No such extent available. */
        return -1;
      }

      long currentBlockCount = cur.getBlockCount();
      currentExtentLength = currentBlockCount * allocationBlockSize;

      if (bytesToSkip >= currentExtentLength) {
        bytesToSkip -= currentExtentLength;
        curLogicalBlock += currentBlockCount;
      } else {
        offset =
            fsOffset
                + firstBlockByteOffset
                + (cur.getStartBlock() * allocationBlockSize)
                + bytesToSkip;
        break;
      }
    }
    // System.out.println("done. skipped " + extIndex + " extents.");

    if (logicalPosition != lastLogicalPos) {
      // System.out.print("ForkFilter.read: (1)seeking to " + offset + "...");
      sourceFile.seek(offset); // Seek to the correct position
      // System.out.println("done.");
    } else if (sourceFile.getFilePointer() != lastPhysicalPos) {
      // System.out.print("ForkFilter.read: (2)seeking to " + offset + "...");
      sourceFile.seek(lastPhysicalPos);
      // System.out.println("done.");
    }

    long bytesLeftInStream = forkLength - logicalPosition;
    // System.err.println("bytesLeftInStream: " + bytesLeftInStream + " len: " + len);
    int totalBytesToRead = bytesLeftInStream < len ? (int) bytesLeftInStream : len;
    int bytesLeftToRead = totalBytesToRead;
    // System.err.println("bytesLeftToRead: " + bytesLeftToRead);
    // Start reading. Extent by extent if needed.
    for (; ; ++extIndex) {
      // System.out.println("ForkFilter.read: reading extent " + extIndex + ".");

      CommonHFSExtentDescriptor cur;
      try {
        cur = getExtent(extIndex, curLogicalBlock);
      } catch (RuntimeException e) {
        if (bytesLeftToRead == totalBytesToRead) {
          throw e;
        } else {
          break;
        }
      }

      sourceFile.seek(
          fsOffset
              + firstBlockByteOffset
              + (cur.getStartBlock() * allocationBlockSize)
              + bytesToSkip);

      long blockCount = cur.getBlockCount();
      long bytesInExtent = blockCount * allocationBlockSize - bytesToSkip;
      int bytesToReadFromExtent =
          (bytesInExtent < bytesLeftToRead) ? (int) bytesInExtent : bytesLeftToRead;

      int bytesReadFromExtent = 0;
      while (bytesReadFromExtent < bytesToReadFromExtent) {
        int bytesToRead = bytesToReadFromExtent - bytesReadFromExtent;
        int positionInArray = pos + (totalBytesToRead - bytesLeftToRead) + bytesReadFromExtent;

        int bytesRead = sourceFile.read(data, positionInArray, bytesToRead);
        if (bytesRead > 0) bytesReadFromExtent += bytesRead;
        else {
          // Update tracker variables before returning
          lastPhysicalPos = sourceFile.getFilePointer();
          int totalBytesRead = positionInArray - pos;
          logicalPosition += totalBytesRead;
          return totalBytesRead;
        }
      }

      bytesLeftToRead -= bytesReadFromExtent;
      bytesToSkip = 0;
      curLogicalBlock += blockCount;

      if (bytesLeftToRead == 0) break;
    }

    // Update tracker variables before returning
    lastPhysicalPos = sourceFile.getFilePointer();
    logicalPosition += totalBytesToRead - bytesLeftToRead;

    if (bytesLeftToRead < totalBytesToRead) {
      int bytesRead = totalBytesToRead - bytesLeftToRead;
      // System.err.println("final bytesRead: " + bytesRead);
      return bytesRead;
    } else return -1;
  }