org.tukaani.xz.XZ Java Examples

The following examples show how to use org.tukaani.xz.XZ. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Check.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
public static Check getInstance(int checkType)
        throws UnsupportedOptionsException {
    switch (checkType) {
        case XZ.CHECK_NONE:
            return new None();

        case XZ.CHECK_CRC32:
            return new CRC32();

        case XZ.CHECK_CRC64:
            return new CRC64();

        case XZ.CHECK_SHA256:
            try {
                return new SHA256();
            } catch (java.security.NoSuchAlgorithmException e) {}

            break;
    }

    throw new UnsupportedOptionsException(
            "Unsupported Check ID " + checkType);
}
 
Example #2
Source File: DecoderUtil.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
public static StreamFlags decodeStreamHeader(byte[] buf)
        throws IOException {
    for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i)
        if (buf[i] != XZ.HEADER_MAGIC[i])
            throw new XZFormatException();

    if (!isCRC32Valid(buf, XZ.HEADER_MAGIC.length, 2,
                      XZ.HEADER_MAGIC.length + 2))
        throw new CorruptedInputException("XZ Stream Header is corrupt");

    try {
        return decodeStreamFlags(buf, XZ.HEADER_MAGIC.length);
    } catch (UnsupportedOptionsException e) {
        throw new UnsupportedOptionsException(
                "Unsupported options in XZ Stream Header");
    }
}
 
Example #3
Source File: Check.java    From LocalGSMLocationProvider with Apache License 2.0 6 votes vote down vote up
public static Check getInstance(int checkType)
        throws UnsupportedOptionsException {
    switch (checkType) {
        case XZ.CHECK_NONE:
            return new None();

        case XZ.CHECK_CRC32:
            return new CRC32();

        case XZ.CHECK_CRC64:
            return new CRC64();

        case XZ.CHECK_SHA256:
            try {
                return new SHA256();
            } catch (java.security.NoSuchAlgorithmException e) {}

            break;
    }

    throw new UnsupportedOptionsException(
            "Unsupported Check ID " + checkType);
}
 
Example #4
Source File: DecoderUtil.java    From LocalGSMLocationProvider with Apache License 2.0 6 votes vote down vote up
public static StreamFlags decodeStreamHeader(byte[] buf)
        throws IOException {
    for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i)
        if (buf[i] != XZ.HEADER_MAGIC[i])
            throw new XZFormatException();

    if (!isCRC32Valid(buf, XZ.HEADER_MAGIC.length, 2,
                      XZ.HEADER_MAGIC.length + 2))
        throw new CorruptedInputException("XZ Stream Header is corrupt");

    try {
        return decodeStreamFlags(buf, XZ.HEADER_MAGIC.length);
    } catch (UnsupportedOptionsException e) {
        throw new UnsupportedOptionsException(
                "Unsupported options in XZ Stream Header");
    }
}
 
Example #5
Source File: DecoderUtil.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public static StreamFlags decodeStreamFooter(byte[] buf)
        throws IOException {
    if (buf[10] != XZ.FOOTER_MAGIC[0] || buf[11] != XZ.FOOTER_MAGIC[1]) {
        // NOTE: The exception could be XZFormatException too.
        // It depends on the situation which one is better.
        throw new CorruptedInputException("XZ Stream Footer is corrupt");
    }

    if (!isCRC32Valid(buf, 4, 6, 0))
        throw new CorruptedInputException("XZ Stream Footer is corrupt");

    StreamFlags streamFlags;
    try {
        streamFlags = decodeStreamFlags(buf, 8);
    } catch (UnsupportedOptionsException e) {
        throw new UnsupportedOptionsException(
                "Unsupported options in XZ Stream Footer");
    }

    streamFlags.backwardSize = 0;
    for (int i = 0; i < 4; ++i)
        streamFlags.backwardSize |= (buf[i + 4] & 0xFF) << (i * 8);

    streamFlags.backwardSize = (streamFlags.backwardSize + 1) * 4;

    return streamFlags;
}
 
Example #6
Source File: DecoderUtil.java    From LocalGSMLocationProvider with Apache License 2.0 5 votes vote down vote up
public static StreamFlags decodeStreamFooter(byte[] buf)
        throws IOException {
    if (buf[10] != XZ.FOOTER_MAGIC[0] || buf[11] != XZ.FOOTER_MAGIC[1]) {
        // NOTE: The exception could be XZFormatException too.
        // It depends on the situation which one is better.
        throw new CorruptedInputException("XZ Stream Footer is corrupt");
    }

    if (!isCRC32Valid(buf, 4, 6, 0))
        throw new CorruptedInputException("XZ Stream Footer is corrupt");

    StreamFlags streamFlags;
    try {
        streamFlags = decodeStreamFlags(buf, 8);
    } catch (UnsupportedOptionsException e) {
        throw new UnsupportedOptionsException(
                "Unsupported options in XZ Stream Footer");
    }

    streamFlags.backwardSize = 0;
    for (int i = 0; i < 4; ++i)
        streamFlags.backwardSize |= (buf[i + 4] & 0xFF) << (i * 8);

    streamFlags.backwardSize = (streamFlags.backwardSize + 1) * 4;

    return streamFlags;
}
 
Example #7
Source File: XzStepTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testXzStep() throws IOException {
  Path sourceFile =
      TestDataHelper.getTestDataScenario(this, "compression_test").resolve("step.data");
  File destinationFile = tmp.newFile("step.data.xz");

  XzStep step =
      new XzStep(
          TestProjectFilesystems.createProjectFilesystem(tmp.getRoot().toPath()),
          sourceFile,
          destinationFile.toPath(),
          /* compressionLevel -- for faster testing */ 1,
          /* keep */ true,
          XZ.CHECK_CRC32);

  ExecutionContext context = TestExecutionContext.newInstance();

  assertEquals(0, step.execute(context).getExitCode());

  ByteSource original = PathByteSource.asByteSource(sourceFile);
  ByteSource decompressed =
      new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
          return new XZInputStream(new FileInputStream(destinationFile));
        }
      };

  assertTrue(Files.exists(sourceFile));
  assertTrue(
      "Decompressed file must be identical to original.", original.contentEquals(decompressed));
}
 
Example #8
Source File: XzStepTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testXzStepDeletesOriginal() throws IOException {
  Path sourceFileOriginal =
      TestDataHelper.getTestDataScenario(this, "compression_test").resolve("step.data");
  Path sourceFile = tmp.newFile("step.data").toPath();
  Files.copy(sourceFileOriginal, sourceFile, StandardCopyOption.REPLACE_EXISTING);
  File destinationFile = tmp.newFile("step.data.xz");

  XzStep step =
      new XzStep(
          TestProjectFilesystems.createProjectFilesystem(tmp.getRoot().toPath()),
          sourceFile,
          destinationFile.toPath(),
          /* compressionLevel -- for faster testing */ 1,
          /* keep */ false,
          XZ.CHECK_CRC32);

  ExecutionContext context = TestExecutionContext.newInstance();

  assertEquals(0, step.execute(context).getExitCode());

  ByteSource original = PathByteSource.asByteSource(sourceFileOriginal);
  ByteSource decompressed =
      new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
          return new XZInputStream(new FileInputStream(destinationFile));
        }
      };

  assertFalse(Files.exists(sourceFile));
  assertTrue(
      "Decompressed file must be identical to original.", original.contentEquals(decompressed));
}
 
Example #9
Source File: XzStep.java    From buck with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an XzStep to compress a file with XZ at a user supplied compression level .
 *
 * <p>The destination file will be {@code sourceFile} with the added {@code .xz} extension.
 *
 * <p>Decompression will require up to 64MiB of RAM.
 *
 * @param sourceFile file to compress
 * @param compressionLevel value from 0 to 9. Higher values result in better compression, but also
 *     need more time to compress and will need more RAM to decompress.
 */
public XzStep(ProjectFilesystem filesystem, Path sourceFile, int compressionLevel) {
  this(
      filesystem,
      sourceFile,
      Paths.get(sourceFile + ".xz"),
      compressionLevel,
      /* keep */ false,
      XZ.CHECK_CRC32);
}
 
Example #10
Source File: XzStep.java    From buck with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an XzStep to compress a file with the given XZ compression level and output path.
 *
 * <p>Decompression will require up to 64MiB of RAM.
 *
 * @param sourceFile file to compress
 * @param outputPath the desired output path.
 * @param compressionLevel level of compression (from 0-9)
 */
public XzStep(
    ProjectFilesystem filesystem, Path sourceFile, Path outputPath, int compressionLevel) {
  this(filesystem, sourceFile, outputPath, compressionLevel, /* keep */ false, XZ.CHECK_CRC32);
}