commons-compress-1.8.1

16:34:42.934 INFO  jd.cli.Main - Decompiling commons-compress-1.8.1.jar
package org.apache.commons.compress.archivers;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;

public final class Lister
{
  private static final ArchiveStreamFactory factory = new ArchiveStreamFactory();
  
  public static void main(String[] args)
    throws Exception
  {
    if (args.length == 0)
    {
      usage();
      return;
    }
    System.out.println("Analysing " + args[0]);
    File f = new File(args[0]);
    if (!f.isFile()) {
      System.err.println(f + " doesn't exist or is a directory");
    }
    InputStream fis = new BufferedInputStream(new FileInputStream(f));
    ArchiveInputStream ais;
    ArchiveInputStream ais;
    if (args.length > 1) {
      ais = factory.createArchiveInputStream(args[1], fis);
    } else {
      ais = factory.createArchiveInputStream(fis);
    }
    System.out.println("Created " + ais.toString());
    ArchiveEntry ae;
    while ((ae = ais.getNextEntry()) != null) {
      System.out.println(ae.getName());
    }
    ais.close();
    fis.close();
  }
  
  private static void usage()
  {
    System.out.println("Parameters: archive-name [archive-type]");
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.Lister
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers;

import java.io.IOException;
import java.io.InputStream;

public abstract class ArchiveInputStream
  extends InputStream
{
  private final byte[] SINGLE = new byte[1];
  private static final int BYTE_MASK = 255;
  private long bytesRead = 0L;
  
  public abstract ArchiveEntry getNextEntry()
    throws IOException;
  
  public int read()
    throws IOException
  {
    int num = read(SINGLE, 0, 1);
    return num == -1 ? -1 : SINGLE[0] & 0xFF;
  }
  
  protected void count(int read)
  {
    count(read);
  }
  
  protected void count(long read)
  {
    if (read != -1L) {
      bytesRead += read;
    }
  }
  
  protected void pushedBackBytes(long pushedBack)
  {
    bytesRead -= pushedBack;
  }
  
  @Deprecated
  public int getCount()
  {
    return (int)bytesRead;
  }
  
  public long getBytesRead()
  {
    return bytesRead;
  }
  
  public boolean canReadEntryData(ArchiveEntry archiveEntry)
  {
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.ArchiveInputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public abstract class ArchiveOutputStream
  extends OutputStream
{
  private final byte[] oneByte = new byte[1];
  static final int BYTE_MASK = 255;
  private long bytesWritten = 0L;
  
  public abstract void putArchiveEntry(ArchiveEntry paramArchiveEntry)
    throws IOException;
  
  public abstract void closeArchiveEntry()
    throws IOException;
  
  public abstract void finish()
    throws IOException;
  
  public abstract ArchiveEntry createArchiveEntry(File paramFile, String paramString)
    throws IOException;
  
  public void write(int b)
    throws IOException
  {
    oneByte[0] = ((byte)(b & 0xFF));
    write(oneByte, 0, 1);
  }
  
  protected void count(int written)
  {
    count(written);
  }
  
  protected void count(long written)
  {
    if (written != -1L) {
      bytesWritten += written;
    }
  }
  
  @Deprecated
  public int getCount()
  {
    return (int)bytesWritten;
  }
  
  public long getBytesWritten()
  {
    return bytesWritten;
  }
  
  public boolean canWriteEntryData(ArchiveEntry archiveEntry)
  {
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.ArchiveOutputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.jar;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.JarMarker;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

public class JarArchiveOutputStream
  extends ZipArchiveOutputStream
{
  private boolean jarMarkerAdded = false;
  
  public JarArchiveOutputStream(OutputStream out)
  {
    super(out);
  }
  
  public void putArchiveEntry(ArchiveEntry ze)
    throws IOException
  {
    if (!jarMarkerAdded)
    {
      ((ZipArchiveEntry)ze).addAsFirstExtraField(JarMarker.getInstance());
      jarMarkerAdded = true;
    }
    super.putArchiveEntry(ze);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.jar.JarArchiveOutputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.jar;

import java.security.cert.Certificate;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

public class JarArchiveEntry
  extends ZipArchiveEntry
{
  private final Attributes manifestAttributes = null;
  private final Certificate[] certificates = null;
  
  public JarArchiveEntry(ZipEntry entry)
    throws ZipException
  {
    super(entry);
  }
  
  public JarArchiveEntry(String name)
  {
    super(name);
  }
  
  public JarArchiveEntry(ZipArchiveEntry entry)
    throws ZipException
  {
    super(entry);
  }
  
  public JarArchiveEntry(JarEntry entry)
    throws ZipException
  {
    super(entry);
  }
  
  @Deprecated
  public Attributes getManifestAttributes()
  {
    return manifestAttributes;
  }
  
  @Deprecated
  public Certificate[] getCertificates()
  {
    if (certificates != null)
    {
      Certificate[] certs = new Certificate[certificates.length];
      System.arraycopy(certificates, 0, certs, 0, certs.length);
      return certs;
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.jar.JarArchiveEntry
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.jar;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

public class JarArchiveInputStream
  extends ZipArchiveInputStream
{
  public JarArchiveInputStream(InputStream inputStream)
  {
    super(inputStream);
  }
  
  public JarArchiveEntry getNextJarEntry()
    throws IOException
  {
    ZipArchiveEntry entry = getNextZipEntry();
    return entry == null ? null : new JarArchiveEntry(entry);
  }
  
  public ArchiveEntry getNextEntry()
    throws IOException
  {
    return getNextJarEntry();
  }
  
  public static boolean matches(byte[] signature, int length)
  {
    return ZipArchiveInputStream.matches(signature, length);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.jar.JarArchiveInputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers;

import java.util.Date;

public abstract interface ArchiveEntry
{
  public static final long SIZE_UNKNOWN = -1L;
  
  public abstract String getName();
  
  public abstract long getSize();
  
  public abstract boolean isDirectory();
  
  public abstract Date getLastModifiedDate();
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.ArchiveEntry
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.cpio;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipEncoding;
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
import org.apache.commons.compress.utils.ArchiveUtils;

public class CpioArchiveOutputStream
  extends ArchiveOutputStream
  implements CpioConstants
{
  private CpioArchiveEntry entry;
  private boolean closed = false;
  private boolean finished;
  private final short entryFormat;
  private final HashMap<String, CpioArchiveEntry> names = new HashMap();
  private long crc = 0L;
  private long written;
  private final OutputStream out;
  private final int blockSize;
  private long nextArtificalDeviceAndInode = 1L;
  private final ZipEncoding encoding;
  
  public CpioArchiveOutputStream(OutputStream out, short format)
  {
    this(out, format, 512, "US-ASCII");
  }
  
  public CpioArchiveOutputStream(OutputStream out, short format, int blockSize)
  {
    this(out, format, blockSize, "US-ASCII");
  }
  
  public CpioArchiveOutputStream(OutputStream out, short format, int blockSize, String encoding)
  {
    this.out = out;
    switch (format)
    {
    case 1: 
    case 2: 
    case 4: 
    case 8: 
      break;
    case 3: 
    case 5: 
    case 6: 
    case 7: 
    default: 
      throw new IllegalArgumentException("Unknown format: " + format);
    }
    entryFormat = format;
    this.blockSize = blockSize;
    this.encoding = ZipEncodingHelper.getZipEncoding(encoding);
  }
  
  public CpioArchiveOutputStream(OutputStream out)
  {
    this(out, (short)1);
  }
  
  public CpioArchiveOutputStream(OutputStream out, String encoding)
  {
    this(out, (short)1, 512, encoding);
  }
  
  private void ensureOpen()
    throws IOException
  {
    if (closed) {
      throw new IOException("Stream closed");
    }
  }
  
  public void putArchiveEntry(ArchiveEntry entry)
    throws IOException
  {
    if (finished) {
      throw new IOException("Stream has already been finished");
    }
    CpioArchiveEntry e = (CpioArchiveEntry)entry;
    ensureOpen();
    if (this.entry != null) {
      closeArchiveEntry();
    }
    if (e.getTime() == -1L) {
      e.setTime(System.currentTimeMillis() / 1000L);
    }
    short format = e.getFormat();
    if (format != entryFormat) {
      throw new IOException("Header format: " + format + " does not match existing format: " + entryFormat);
    }
    if (names.put(e.getName(), e) != null) {
      throw new IOException("duplicate entry: " + e.getName());
    }
    writeHeader(e);
    this.entry = e;
    written = 0L;
  }
  
  private void writeHeader(CpioArchiveEntry e)
    throws IOException
  {
    switch (e.getFormat())
    {
    case 1: 
      out.write(ArchiveUtils.toAsciiBytes("070701"));
      count(6);
      writeNewEntry(e);
      break;
    case 2: 
      out.write(ArchiveUtils.toAsciiBytes("070702"));
      count(6);
      writeNewEntry(e);
      break;
    case 4: 
      out.write(ArchiveUtils.toAsciiBytes("070707"));
      count(6);
      writeOldAsciiEntry(e);
      break;
    case 8: 
      boolean swapHalfWord = true;
      writeBinaryLong(29127L, 2, swapHalfWord);
      writeOldBinaryEntry(e, swapHalfWord);
      break;
    case 3: 
    case 5: 
    case 6: 
    case 7: 
    default: 
      throw new IOException("unknown format " + e.getFormat());
    }
  }
  
  private void writeNewEntry(CpioArchiveEntry entry)
    throws IOException
  {
    long inode = entry.getInode();
    long devMin = entry.getDeviceMin();
    if ("TRAILER!!!".equals(entry.getName()))
    {
      inode = devMin = 0L;
    }
    else if ((inode == 0L) && (devMin == 0L))
    {
      inode = nextArtificalDeviceAndInode & 0xFFFFFFFFFFFFFFFF;
      devMin = nextArtificalDeviceAndInode++ >> 32 & 0xFFFFFFFFFFFFFFFF;
    }
    else
    {
      nextArtificalDeviceAndInode = (Math.max(nextArtificalDeviceAndInode, inode + 4294967296L * devMin) + 1L);
    }
    writeAsciiLong(inode, 8, 16);
    writeAsciiLong(entry.getMode(), 8, 16);
    writeAsciiLong(entry.getUID(), 8, 16);
    writeAsciiLong(entry.getGID(), 8, 16);
    writeAsciiLong(entry.getNumberOfLinks(), 8, 16);
    writeAsciiLong(entry.getTime(), 8, 16);
    writeAsciiLong(entry.getSize(), 8, 16);
    writeAsciiLong(entry.getDeviceMaj(), 8, 16);
    writeAsciiLong(devMin, 8, 16);
    writeAsciiLong(entry.getRemoteDeviceMaj(), 8, 16);
    writeAsciiLong(entry.getRemoteDeviceMin(), 8, 16);
    writeAsciiLong(entry.getName().length() + 1, 8, 16);
    writeAsciiLong(entry.getChksum(), 8, 16);
    writeCString(entry.getName());
    pad(entry.getHeaderPadCount());
  }
  
  private void writeOldAsciiEntry(CpioArchiveEntry entry)
    throws IOException
  {
    long inode = entry.getInode();
    long device = entry.getDevice();
    if ("TRAILER!!!".equals(entry.getName()))
    {
      inode = device = 0L;
    }
    else if ((inode == 0L) && (device == 0L))
    {
      inode = nextArtificalDeviceAndInode & 0x3FFFF;
      device = nextArtificalDeviceAndInode++ >> 18 & 0x3FFFF;
    }
    else
    {
      nextArtificalDeviceAndInode = (Math.max(nextArtificalDeviceAndInode, inode + 262144L * device) + 1L);
    }
    writeAsciiLong(device, 6, 8);
    writeAsciiLong(inode, 6, 8);
    writeAsciiLong(entry.getMode(), 6, 8);
    writeAsciiLong(entry.getUID(), 6, 8);
    writeAsciiLong(entry.getGID(), 6, 8);
    writeAsciiLong(entry.getNumberOfLinks(), 6, 8);
    writeAsciiLong(entry.getRemoteDevice(), 6, 8);
    writeAsciiLong(entry.getTime(), 11, 8);
    writeAsciiLong(entry.getName().length() + 1, 6, 8);
    writeAsciiLong(entry.getSize(), 11, 8);
    writeCString(entry.getName());
  }
  
  private void writeOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord)
    throws IOException
  {
    long inode = entry.getInode();
    long device = entry.getDevice();
    if ("TRAILER!!!".equals(entry.getName()))
    {
      inode = device = 0L;
    }
    else if ((inode == 0L) && (device == 0L))
    {
      inode = nextArtificalDeviceAndInode & 0xFFFF;
      device = nextArtificalDeviceAndInode++ >> 16 & 0xFFFF;
    }
    else
    {
      nextArtificalDeviceAndInode = (Math.max(nextArtificalDeviceAndInode, inode + 65536L * device) + 1L);
    }
    writeBinaryLong(device, 2, swapHalfWord);
    writeBinaryLong(inode, 2, swapHalfWord);
    writeBinaryLong(entry.getMode(), 2, swapHalfWord);
    writeBinaryLong(entry.getUID(), 2, swapHalfWord);
    writeBinaryLong(entry.getGID(), 2, swapHalfWord);
    writeBinaryLong(entry.getNumberOfLinks(), 2, swapHalfWord);
    writeBinaryLong(entry.getRemoteDevice(), 2, swapHalfWord);
    writeBinaryLong(entry.getTime(), 4, swapHalfWord);
    writeBinaryLong(entry.getName().length() + 1, 2, swapHalfWord);
    writeBinaryLong(entry.getSize(), 4, swapHalfWord);
    writeCString(entry.getName());
    pad(entry.getHeaderPadCount());
  }
  
  public void closeArchiveEntry()
    throws IOException
  {
    if (finished) {
      throw new IOException("Stream has already been finished");
    }
    ensureOpen();
    if (entry == null) {
      throw new IOException("Trying to close non-existent entry");
    }
    if (entry.getSize() != written) {
      throw new IOException("invalid entry size (expected " + entry.getSize() + " but got " + written + " bytes)");
    }
    pad(entry.getDataPadCount());
    if ((entry.getFormat() == 2) && (crc != entry.getChksum())) {
      throw new IOException("CRC Error");
    }
    entry = null;
    crc = 0L;
    written = 0L;
  }
  
  public void write(byte[] b, int off, int len)
    throws IOException
  {
    ensureOpen();
    if ((off < 0) || (len < 0) || (off > b.length - len)) {
      throw new IndexOutOfBoundsException();
    }
    if (len == 0) {
      return;
    }
    if (entry == null) {
      throw new IOException("no current CPIO entry");
    }
    if (written + len > entry.getSize()) {
      throw new IOException("attempt to write past end of STORED entry");
    }
    out.write(b, off, len);
    written += len;
    if (entry.getFormat() == 2) {
      for (int pos = 0; pos < len; pos++) {
        crc += (b[pos] & 0xFF);
      }
    }
    count(len);
  }
  
  public void finish()
    throws IOException
  {
    ensureOpen();
    if (finished) {
      throw new IOException("This archive has already been finished");
    }
    if (entry != null) {
      throw new IOException("This archive contains unclosed entries.");
    }
    entry = new CpioArchiveEntry(entryFormat);
    entry.setName("TRAILER!!!");
    entry.setNumberOfLinks(1L);
    writeHeader(entry);
    closeArchiveEntry();
    
    int lengthOfLastBlock = (int)(getBytesWritten() % blockSize);
    if (lengthOfLastBlock != 0) {
      pad(blockSize - lengthOfLastBlock);
    }
    finished = true;
  }
  
  public void close()
    throws IOException
  {
    if (!finished) {
      finish();
    }
    if (!closed)
    {
      out.close();
      closed = true;
    }
  }
  
  private void pad(int count)
    throws IOException
  {
    if (count > 0)
    {
      byte[] buff = new byte[count];
      out.write(buff);
      count(count);
    }
  }
  
  private void writeBinaryLong(long number, int length, boolean swapHalfWord)
    throws IOException
  {
    byte[] tmp = CpioUtil.long2byteArray(number, length, swapHalfWord);
    out.write(tmp);
    count(tmp.length);
  }
  
  private void writeAsciiLong(long number, int length, int radix)
    throws IOException
  {
    StringBuilder tmp = new StringBuilder();
    if (radix == 16) {
      tmp.append(Long.toHexString(number));
    } else if (radix == 8) {
      tmp.append(Long.toOctalString(number));
    } else {
      tmp.append(Long.toString(number));
    }
    String tmpStr;
    String tmpStr;
    if (tmp.length() <= length)
    {
      long insertLength = length - tmp.length();
      for (int pos = 0; pos < insertLength; pos++) {
        tmp.insert(0, "0");
      }
      tmpStr = tmp.toString();
    }
    else
    {
      tmpStr = tmp.substring(tmp.length() - length);
    }
    byte[] b = ArchiveUtils.toAsciiBytes(tmpStr);
    out.write(b);
    count(b.length);
  }
  
  private void writeCString(String str)
    throws IOException
  {
    ByteBuffer buf = encoding.encode(str);
    int len = buf.limit() - buf.position();
    out.write(buf.array(), buf.arrayOffset(), len);
    out.write(0);
    count(len + 1);
  }
  
  public ArchiveEntry createArchiveEntry(File inputFile, String entryName)
    throws IOException
  {
    if (finished) {
      throw new IOException("Stream has already been finished");
    }
    return new CpioArchiveEntry(inputFile, entryName);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.cpio;

public abstract interface CpioConstants
{
  public static final String MAGIC_NEW = "070701";
  public static final String MAGIC_NEW_CRC = "070702";
  public static final String MAGIC_OLD_ASCII = "070707";
  public static final int MAGIC_OLD_BINARY = 29127;
  public static final short FORMAT_NEW = 1;
  public static final short FORMAT_NEW_CRC = 2;
  public static final short FORMAT_OLD_ASCII = 4;
  public static final short FORMAT_OLD_BINARY = 8;
  public static final short FORMAT_NEW_MASK = 3;
  public static final short FORMAT_OLD_MASK = 12;
  public static final int S_IFMT = 61440;
  public static final int C_ISSOCK = 49152;
  public static final int C_ISLNK = 40960;
  public static final int C_ISNWK = 36864;
  public static final int C_ISREG = 32768;
  public static final int C_ISBLK = 24576;
  public static final int C_ISDIR = 16384;
  public static final int C_ISCHR = 8192;
  public static final int C_ISFIFO = 4096;
  public static final int C_ISUID = 2048;
  public static final int C_ISGID = 1024;
  public static final int C_ISVTX = 512;
  public static final int C_IRUSR = 256;
  public static final int C_IWUSR = 128;
  public static final int C_IXUSR = 64;
  public static final int C_IRGRP = 32;
  public static final int C_IWGRP = 16;
  public static final int C_IXGRP = 8;
  public static final int C_IROTH = 4;
  public static final int C_IWOTH = 2;
  public static final int C_IXOTH = 1;
  public static final String CPIO_TRAILER = "TRAILER!!!";
  public static final int BLOCK_SIZE = 512;
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.cpio.CpioConstants
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.cpio;

class CpioUtil
{
  static long fileType(long mode)
  {
    return mode & 0xF000;
  }
  
  static long byteArray2long(byte[] number, boolean swapHalfWord)
  {
    if (number.length % 2 != 0) {
      throw new UnsupportedOperationException();
    }
    long ret = 0L;
    int pos = 0;
    byte[] tmp_number = new byte[number.length];
    System.arraycopy(number, 0, tmp_number, 0, number.length);
    if (!swapHalfWord)
    {
      byte tmp = 0;
      for (pos = 0; pos < tmp_number.length; pos++)
      {
        tmp = tmp_number[pos];
        tmp_number[(pos++)] = tmp_number[pos];
        tmp_number[pos] = tmp;
      }
    }
    ret = tmp_number[0] & 0xFF;
    for (pos = 1; pos < tmp_number.length; pos++)
    {
      ret <<= 8;
      ret |= tmp_number[pos] & 0xFF;
    }
    return ret;
  }
  
  static byte[] long2byteArray(long number, int length, boolean swapHalfWord)
  {
    byte[] ret = new byte[length];
    int pos = 0;
    long tmp_number = 0L;
    if ((length % 2 != 0) || (length < 2)) {
      throw new UnsupportedOperationException();
    }
    tmp_number = number;
    for (pos = length - 1; pos >= 0; pos--)
    {
      ret[pos] = ((byte)(int)(tmp_number & 0xFF));
      tmp_number >>= 8;
    }
    if (!swapHalfWord)
    {
      byte tmp = 0;
      for (pos = 0; pos < length; pos++)
      {
        tmp = ret[pos];
        ret[(pos++)] = ret[pos];
        ret[pos] = tmp;
      }
    }
    return ret;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.cpio.CpioUtil
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.cpio;

import java.io.File;
import java.util.Date;
import org.apache.commons.compress.archivers.ArchiveEntry;

public class CpioArchiveEntry
  implements CpioConstants, ArchiveEntry
{
  private final short fileFormat;
  private final int headerSize;
  private final int alignmentBoundary;
  private long chksum = 0L;
  private long filesize = 0L;
  private long gid = 0L;
  private long inode = 0L;
  private long maj = 0L;
  private long min = 0L;
  private long mode = 0L;
  private long mtime = 0L;
  private String name;
  private long nlink = 0L;
  private long rmaj = 0L;
  private long rmin = 0L;
  private long uid = 0L;
  
  public CpioArchiveEntry(short format)
  {
    switch (format)
    {
    case 1: 
      headerSize = 110;
      alignmentBoundary = 4;
      break;
    case 2: 
      headerSize = 110;
      alignmentBoundary = 4;
      break;
    case 4: 
      headerSize = 76;
      alignmentBoundary = 0;
      break;
    case 8: 
      headerSize = 26;
      alignmentBoundary = 2;
      break;
    case 3: 
    case 5: 
    case 6: 
    case 7: 
    default: 
      throw new IllegalArgumentException("Unknown header type");
    }
    fileFormat = format;
  }
  
  public CpioArchiveEntry(String name)
  {
    this((short)1, name);
  }
  
  public CpioArchiveEntry(short format, String name)
  {
    this(format);
    this.name = name;
  }
  
  public CpioArchiveEntry(String name, long size)
  {
    this(name);
    setSize(size);
  }
  
  public CpioArchiveEntry(short format, String name, long size)
  {
    this(format, name);
    setSize(size);
  }
  
  public CpioArchiveEntry(File inputFile, String entryName)
  {
    this((short)1, inputFile, entryName);
  }
  
  public CpioArchiveEntry(short format, File inputFile, String entryName)
  {
    this(format, entryName, inputFile.isFile() ? inputFile.length() : 0L);
    if (inputFile.isDirectory()) {
      setMode(16384L);
    } else if (inputFile.isFile()) {
      setMode(32768L);
    } else {
      throw new IllegalArgumentException("Cannot determine type of file " + inputFile.getName());
    }
    setTime(inputFile.lastModified() / 1000L);
  }
  
  private void checkNewFormat()
  {
    if ((fileFormat & 0x3) == 0) {
      throw new UnsupportedOperationException();
    }
  }
  
  private void checkOldFormat()
  {
    if ((fileFormat & 0xC) == 0) {
      throw new UnsupportedOperationException();
    }
  }
  
  public long getChksum()
  {
    checkNewFormat();
    return chksum;
  }
  
  public long getDevice()
  {
    checkOldFormat();
    return min;
  }
  
  public long getDeviceMaj()
  {
    checkNewFormat();
    return maj;
  }
  
  public long getDeviceMin()
  {
    checkNewFormat();
    return min;
  }
  
  public long getSize()
  {
    return filesize;
  }
  
  public short getFormat()
  {
    return fileFormat;
  }
  
  public long getGID()
  {
    return gid;
  }
  
  public int getHeaderSize()
  {
    return headerSize;
  }
  
  public int getAlignmentBoundary()
  {
    return alignmentBoundary;
  }
  
  public int getHeaderPadCount()
  {
    if (alignmentBoundary == 0) {
      return 0;
    }
    int size = headerSize + 1;
    if (name != null) {
      size += name.length();
    }
    int remain = size % alignmentBoundary;
    if (remain > 0) {
      return alignmentBoundary - remain;
    }
    return 0;
  }
  
  public int getDataPadCount()
  {
    if (alignmentBoundary == 0) {
      return 0;
    }
    long size = filesize;
    int remain = (int)(size % alignmentBoundary);
    if (remain > 0) {
      return alignmentBoundary - remain;
    }
    return 0;
  }
  
  public long getInode()
  {
    return inode;
  }
  
  public long getMode()
  {
    return (mode == 0L) && (!"TRAILER!!!".equals(name)) ? 32768L : mode;
  }
  
  public String getName()
  {
    return name;
  }
  
  public long getNumberOfLinks()
  {
    return nlink == 0L ? 1L : isDirectory() ? 2L : nlink;
  }
  
  public long getRemoteDevice()
  {
    checkOldFormat();
    return rmin;
  }
  
  public long getRemoteDeviceMaj()
  {
    checkNewFormat();
    return rmaj;
  }
  
  public long getRemoteDeviceMin()
  {
    checkNewFormat();
    return rmin;
  }
  
  public long getTime()
  {
    return mtime;
  }
  
  public Date getLastModifiedDate()
  {
    return new Date(1000L * getTime());
  }
  
  public long getUID()
  {
    return uid;
  }
  
  public boolean isBlockDevice()
  {
    return CpioUtil.fileType(mode) == 24576L;
  }
  
  public boolean isCharacterDevice()
  {
    return CpioUtil.fileType(mode) == 8192L;
  }
  
  public boolean isDirectory()
  {
    return CpioUtil.fileType(mode) == 16384L;
  }
  
  public boolean isNetwork()
  {
    return CpioUtil.fileType(mode) == 36864L;
  }
  
  public boolean isPipe()
  {
    return CpioUtil.fileType(mode) == 4096L;
  }
  
  public boolean isRegularFile()
  {
    return CpioUtil.fileType(mode) == 32768L;
  }
  
  public boolean isSocket()
  {
    return CpioUtil.fileType(mode) == 49152L;
  }
  
  public boolean isSymbolicLink()
  {
    return CpioUtil.fileType(mode) == 40960L;
  }
  
  public void setChksum(long chksum)
  {
    checkNewFormat();
    this.chksum = chksum;
  }
  
  public void setDevice(long device)
  {
    checkOldFormat();
    min = device;
  }
  
  public void setDeviceMaj(long maj)
  {
    checkNewFormat();
    this.maj = maj;
  }
  
  public void setDeviceMin(long min)
  {
    checkNewFormat();
    this.min = min;
  }
  
  public void setSize(long size)
  {
    if ((size < 0L) || (size > 4294967295L)) {
      throw new IllegalArgumentException("invalid entry size <" + size + ">");
    }
    filesize = size;
  }
  
  public void setGID(long gid)
  {
    this.gid = gid;
  }
  
  public void setInode(long inode)
  {
    this.inode = inode;
  }
  
  public void setMode(long mode)
  {
    long maskedMode = mode & 0xF000;
    switch ((int)maskedMode)
    {
    case 4096: 
    case 8192: 
    case 16384: 
    case 24576: 
    case 32768: 
    case 36864: 
    case 40960: 
    case 49152: 
      break;
    default: 
      throw new IllegalArgumentException("Unknown mode. Full: " + Long.toHexString(mode) + " Masked: " + Long.toHexString(maskedMode));
    }
    this.mode = mode;
  }
  
  public void setName(String name)
  {
    this.name = name;
  }
  
  public void setNumberOfLinks(long nlink)
  {
    this.nlink = nlink;
  }
  
  public void setRemoteDevice(long device)
  {
    checkOldFormat();
    rmin = device;
  }
  
  public void setRemoteDeviceMaj(long rmaj)
  {
    checkNewFormat();
    this.rmaj = rmaj;
  }
  
  public void setRemoteDeviceMin(long rmin)
  {
    checkNewFormat();
    this.rmin = rmin;
  }
  
  public void setTime(long time)
  {
    mtime = time;
  }
  
  public void setUID(long uid)
  {
    this.uid = uid;
  }
  
  public int hashCode()
  {
    int prime = 31;
    int result = 1;
    result = 31 * result + (name == null ? 0 : name.hashCode());
    return result;
  }
  
  public boolean equals(Object obj)
  {
    if (this == obj) {
      return true;
    }
    if ((obj == null) || (getClass() != obj.getClass())) {
      return false;
    }
    CpioArchiveEntry other = (CpioArchiveEntry)obj;
    if (name == null)
    {
      if (name != null) {
        return false;
      }
    }
    else if (!name.equals(name)) {
      return false;
    }
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.compress.archivers.cpio.CpioArchiveEntry
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.compress.archivers.cpio;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipEncoding;
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
import org.apache.commons.compress.utils.ArchiveUtils;
import org.apache.commons.compress.utils.IOUtils;

public class CpioArchiveInputStream
  extends ArchiveInputStream
  implements CpioConstants
{
  private boolean closed = false;
  private CpioArchiveEntry entry;
  private long entryBytesRead = 0L;
  private boolean entryEOF = false;
  private final byte[] tmpbuf = new byte['?'];
  private long crc = 0L;
  private final InputStream in;
  private final byte[] TWO_BYTES_BUF = new byte[2];
  private final byte[] FOUR_BYTES_BUF = new byte[4];
  private final byte[] SIX_BYTES_BUF = new byte[6];
  private final int blockSize;
  private final ZipEncoding encoding;
  
  public CpioArchiveInputStream(InputStream in)
  {
    this(in, 512, "US-ASCII");
  }
  
  public CpioArchiveInputStream(InputStream in, String encoding)
  {
    this(in, 512, encoding);
  }
  
  public CpioArchiveInputStream(InputStream in, int blockSize)
  {
    this(in, blockSize, "US-ASCII");
  }
  
  public CpioArchiveInputStream(InputStream in, int blockSize, String encoding)
  {
    this.in = in;
    this.blockSize = blockSize;
    this.encoding = ZipEncodingHelper.getZipEncoding(encoding);
  }
  
  public int available()
    throws IOException
  {
    ensureOpen();
    if (entryEOF) {
      return 0;
    }
    return 1;
  }
  
  public void close()
    throws IOException
  {
    if (!closed)
    {
      in.close();
      closed = true;
    }
  }
  
  private void closeEntry()
    throws IOException
  {
    while (skip(2147483647L) == 2147483647L) {}
  }
  
  private void ensureOpen()
    throws IOException
  {
    if (closed) {
      throw new IOException("Stream closed");
    }
  }
  
  public CpioArchiveEntry getNextCPIOEntry()
    throws IOException
  {
    ensureOpen();
    if (entry != null) {
      closeEntry();
    }
    readFully(TWO_BYTES_BUF, 0, TWO_BYTES_BUF.length);
    if (CpioUtil.byteArray2long(TWO_BYTES_BUF, false) == 29127L)
    {
      entry = readOldBinaryEntry(false);
    }
    else if (CpioUtil.byteArray2long(TWO_BYTES_BUF, true) == 29127L)
    {
      entry = readOldBinaryEntry(true);
    }
    else
    {
      System.arraycopy(TWO_BYTES_BUF, 0, SIX_BYTES_BUF, 0, TWO_BYTES_BUF.length);
      
      readFully(SIX_BYTES_BUF, TWO_BYTES_BUF.length, FOUR_BYTES_BUF.length);
      
      String magicString = ArchiveUtils.toAsciiString(SIX_BYTES_BUF);
      if (magicString.equals("070701")) {
        entry = readNewEntry(false);
      } else if (magicString.equals("070702")) {
        entry = readNewEntry(true);
      } else if (magicString.equals("070707")) {
        entry = readOldAsciiEntry();
      } else {
        throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getBytesRead());
      }
    }
    entryBytesRead = 0L;
    entryEOF = false;
    crc = 0L;
    if (entry.getName().equals("TRAILER!!!"))
    {
      entryEOF = true;
      skipRemainderOfLastBlock();
      return null;
    }
    return entry;
  }
  
  private void skip(int bytes)
    throws IOException
  {
    if (bytes > 0) {
      readFully(FOUR_BYTES_BUF, 0, bytes);
    }
  }
  
  public int read(byte[] b, int off, int len)
    throws IOException
  {
    ensureOpen();
    if ((off < 0) || (len < 0) || (off > b.length - len)) {
      throw new IndexOutOfBoundsException();
    }
    if (len == 0) {
      return 0;
    }
    if ((entry == null) || (entryEOF)) {
      return -1;
    }
    if (entryBytesRead == entry.getSize())
    {
      skip(entry.getDataPadCount());
      entryEOF = true;
      if ((entry.getFormat() == 2) && (crc != entry.getChksum())) {
        throw new IOException("CRC Error. Occured at byte: " + getBytesRead());
      }
      return -1;
    }
    int tmplength = (int)Math.min(len, entry.getSize() - entryBytesRead);
    if (tmplength < 0) {
      return -1;
    }
    int tmpread = readFully(b, off, tmplength);
    if (entry.getFormat() == 2) {
      for (int pos = 0; pos < tmpread; pos++) {
        crc += (b[pos] & 0xFF);
      }
    }
    entryBytesRead += tmpread;
    
    return tmpread;
  }
  
  private final int readFully(byte[] b, int off, int len)
    throws IOException
  {
    int count = IOUtils.readFully(in, b, off, len);
    count(count);
    if (count < len) {
      throw new EOFException();
    }
    return count;
  }
  
  private long readBinaryLong(int length, boolean swapHalfWord)
    throws IOException
  {
    byte[] tmp = new byte[length];
    readFully(tmp, 0, tmp.length);
    return CpioUtil.byteArray2long(tmp, swapHalfWord);
  }
  
  private long readAsciiLong(int length, int radix)
    throws IOException
  {
    byte[] tmpBuffer = new byte[length];
    readFully(tmpBuffer, 0, tmpBuffer.length);
    return Long.parseLong(ArchiveUtils.toAsciiString(tmpBuffer), radix);
  }
  
  private CpioArchiveEntry readNewEntry(boolean hasCrc)
    throws IOException
  {
    CpioArchiveEntry ret;
    CpioArchiveEntry ret;
    if (hasCrc) {
      ret = new CpioArchiveEntry((short)2);
    } else {
      ret = new CpioArchiveEntry((short)1);
    }
    ret.setInode(readAsciiLong(8, 16));
    long mode = readAsciiLong(8, 16);
    if (CpioUtil.fileType(mode) != 0L) {
      ret.setMode(mode);
    }
    ret.setUID(readAsciiLong(8, 16));
    ret.setGID(readAsciiLong(8, 16));
    ret.setNumberOfLinks(readAsciiLong(8, 16));
    ret.setTime(readAsciiLong(8, 16));
    ret.setSize(readAsciiLong(8, 16));
    ret.setDeviceMaj(readAsciiLong(8, 16));
    ret.setDeviceMin(readAsciiLong(8, 16));
    ret.setRemoteDeviceMaj(readAsciiLong(8, 16));
    ret.setRemoteDeviceMin(readAsciiLong(8, 16));
    long namesize = readAsciiLong(8, 16);
    ret.setChksum(readAsciiLong(8, 16));
    String name = readCString((int)namesize);
    ret.setName(name);
    if ((CpioUtil.fileType(mode) == 0L) && (!name.equals("TRAILER!!!"))) {
      throw new IOException("Mode 0 only allowed in the trailer. Found entry name: " + name + " Occured at byte: " + getBytesRead());
    }
    skip(ret.getHeaderPadCount());
    
    return ret;
  }
  
  private CpioArchiveEntry readOldAsciiEntry()
    throws IOException
  {
    CpioArchiveEntry ret = new CpioArchiveEntry((short)4);
    
    ret.setDevice(readAsciiLong(6, 8));
    ret.setInode(readAsciiLong(6, 8));
    long mode = readAsciiLong(6, 8);
    if (CpioUtil.fileType(mode) != 0L) {
      ret.setMode(mode);
    }
    ret.setUID(readAsciiLong(6, 8));
    ret.setGID(readAsciiLong(6, 8));
    ret.setNumberOfLinks(readAsciiLong(6, 8));
    ret.setRemoteDevice(readAsciiLong(6, 8));
    ret.setTime(readAsciiLong(11, 8));
    long namesize = readAsciiLong(6, 8);
    ret.setSize(readAsciiLong(11, 8));
    String name = readCString((int)namesize);
    ret.setName(name);
    if ((CpioUtil.fileType(mode) == 0L) && (!name.equals("TRAILER!!!"))) {
      throw new IOException("Mode 0 only allowed in the trailer. Found entry: " + name + " Occured at byte: " + getBytesRead());
    }
    return ret;
  }
  
  private CpioArchiveEntry readOldBinaryEntry(boolean swapHalfWord)
    throws IOException
  {
    CpioArchiveEntry ret = new CpioArchiveEntry((short)8);
    
    ret.setDevice(readBinaryLong(2, swapHalfWord));
    ret.setInode(readBinaryLong(2, swapHalfWord));
    long mode = readBinaryLong(2, swapHalfWord);
    if (CpioUtil.fileType(mode) != 0L) {
      ret.setMode(mode);
    }
    ret.setUID(readBinaryLong(2, swapHalfWord));
    ret.setGID(readBinaryLong(2, swapHalfWord));
    ret.setNumberOfLinks(readBinaryLong(2, swapHalfWord));
    ret.setRemoteDevice(readBinaryLong(2, swapHalfWord));
    ret.setTime(readBinaryLong(4, swapHalfWord));
    long namesize = readBinaryLong(2, swapHalfWord);
    ret.setSize(readBinaryLong(4, swapHalfWord));
    String name = readCString((int)namesize);
    ret.setName(name);
    if ((CpioUtil.fileType(mode) == 0L) && (!name.equals("TRAILER!!!"))) {
      throw new IOException("Mode 0 only allowed in the trailer. Found entry: " + name + "Occured at byte: " + getBytesRead());
    }
    skip(ret.getHeaderPadCount());
    
    return ret;
  }
  
  private String readCString(int length)
    throws IOException
  {
    byte[] tmpBuffer = new byte[length - 1];
    readFully(tmpBuffer, 0, tmpBuffer.length);
    in.read();
    return encoding.decode(tmpBuffer);
  }
  
  public long skip(long n)
    throws IOException
  {
    if (n < 0L) {
      throw new IllegalArgumentException("negative skip length");
    }
    ensureOpen();
    int max = (int)Math.min(n, 2147483647L);
    int total = 0;
    while (total < max)
    {
      int len = max - total;
      if (len > tmpbuf.length) {
        len = tmpbuf.length;
      }
      len = read(tmpbuf, 0, len);
      if (len == -1)
      {
        entryEOF = true;
        break;
      }
      total += len;
    }
    return total;
  }
  
  public ArchiveEntry getNextEntry()
    throws IOException
  {
    return getNextCPIOEntry();
  }
  
  private void skipRemainderOfLastBlock()
    throws IOException
  {
    long readFromLastBlock = getBytesRead() % blockSize;
    long remainingBytes = readFromLastBlock == 0L ? 0L : blockSize - readFromLastBlock;
    while (remainingBytes > 0L)
    {
      long skipped = skip(blockSize - readFromLastBlock);
      if (skipped <= 0L) {
        break;
      }
      remainingBytes -= skipped;
    }
  }
  
  public static boolean matches(byte[] signature, int length)
  {
    if (length < 6) {
      return false;
    }
    if ((signature[0] == 113) && ((signature[1] & 0xFF) == 199)) {
      return true;
    }
    if ((signature[1] == 113) && ((signature[0] & 0xFF) == 199)) {
      return true;
    }
    if (signature[0] != 48) {
      return false;
    }
    if (signature[1] != 55) {
      return false;
    }
    if (signature[2] != 48) {
      return false;
    }
    if (signature[3] != 55) {
      return false;
    }
    if (signature[4] != 48) {
      return false;
    }
    if (signature[5] == 49) {
      return true;
    }
    if (signature[5] == 50) {
      return true;
    }
    if (sign
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd