Example #1
0
  /**
   * Get the next record in this tar archive. This will skip over any remaining data in the current
   * entry, if there is one, and place the input stream at the header of the next entry. If there
   * are no more entries in the archive, null will be returned to indicate that the end of the
   * archive has been reached.
   *
   * @return The next header in the archive, or null.
   * @throws IOException on error
   */
  private byte[] getRecord() throws IOException {
    if (hasHitEOF) {
      return null;
    }

    byte[] headerBuf = buffer.readRecord();

    if (headerBuf == null) {
      hasHitEOF = true;
    } else if (buffer.isEOFRecord(headerBuf)) {
      hasHitEOF = true;
    }

    return hasHitEOF ? null : headerBuf;
  }
Example #2
0
 /**
  * Get the record size being used by this stream's TarBuffer.
  *
  * @return The TarBuffer record size.
  */
 public int getRecordSize() {
   return buffer.getRecordSize();
 }
Example #3
0
 /**
  * Closes this stream. Calls the TarBuffer's close() method.
  *
  * @throws IOException on error
  */
 public void close() throws IOException {
   buffer.close();
 }
Example #4
0
  /**
   * Reads bytes from the current tar archive entry.
   *
   * <p>This method is aware of the boundaries of the current entry in the archive and will deal
   * with them as if they were this stream's start and EOF.
   *
   * @param buf The buffer into which to place bytes read.
   * @param offset The offset at which to place bytes read.
   * @param numToRead The number of bytes to read.
   * @return The number of bytes read, or -1 at EOF.
   * @throws IOException on error
   */
  public int read(byte[] buf, int offset, int numToRead) throws IOException {
    int totalRead = 0;

    if (entryOffset >= entrySize) {
      return -1;
    }

    if ((numToRead + entryOffset) > entrySize) {
      numToRead = (int) (entrySize - entryOffset);
    }

    if (readBuf != null) {
      int sz = (numToRead > readBuf.length) ? readBuf.length : numToRead;

      System.arraycopy(readBuf, 0, buf, offset, sz);

      if (sz >= readBuf.length) {
        readBuf = null;
      } else {
        int newLen = readBuf.length - sz;
        byte[] newBuf = new byte[newLen];

        System.arraycopy(readBuf, sz, newBuf, 0, newLen);

        readBuf = newBuf;
      }

      totalRead += sz;
      numToRead -= sz;
      offset += sz;
    }

    while (numToRead > 0) {
      byte[] rec = buffer.readRecord();

      if (rec == null) {
        // Unexpected EOF!
        throw new IOException(
            "unexpected EOF with "
                + numToRead
                + " bytes unread. Occured at byte: "
                + getBytesRead());
      }
      count(rec.length);
      int sz = numToRead;
      int recLen = rec.length;

      if (recLen > sz) {
        System.arraycopy(rec, 0, buf, offset, sz);

        readBuf = new byte[recLen - sz];

        System.arraycopy(rec, sz, readBuf, 0, recLen - sz);
      } else {
        sz = recLen;

        System.arraycopy(rec, 0, buf, offset, recLen);
      }

      totalRead += sz;
      numToRead -= sz;
      offset += sz;
    }

    entryOffset += totalRead;

    return totalRead;
  }