Example usage for org.apache.commons.codec.binary.yenc YEncFile YEncFile

List of usage examples for org.apache.commons.codec.binary.yenc YEncFile YEncFile

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary.yenc YEncFile YEncFile.

Prototype

public YEncFile(File file) throws YEncException 

Source Link

Usage

From source file:org.ambiance.codec.YEncDecoder.java

/**
 * Decode a single file//from   w  w w .j  a  v a2 s. c o m
 */
public void decode(File input) throws DecoderException {
    try {
        YEncFile yencFile = new YEncFile(input);

        // Get the output file
        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(yencFile.getHeader().getName());
        RandomAccessFile output = new RandomAccessFile(outputName.toString(), "rw");

        // Place the pointer to the begining of data to decode
        long pos = yencFile.getDataBegin();
        yencFile.getInput().seek(pos);

        // Bufferise the file
        // TODO - A Amliorer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (pos < yencFile.getDataEnd()) {
            baos.write(yencFile.getInput().read());
            pos++;
        }

        byte[] buff = decoder.decode(baos.toByteArray());

        // Write and close output
        output.write(buff);
        output.close();

        // Warn if CRC32 check is not OK
        CRC32 crc32 = new CRC32();
        crc32.update(buff);
        if (!yencFile.getTrailer().getCrc32().equals(Long.toHexString(crc32.getValue()).toUpperCase()))
            throw new DecoderException("Error in CRC32 check.");

    } catch (YEncException ye) {
        throw new DecoderException("Input file is not a YEnc file or contains error : " + ye.getMessage());
    } catch (FileNotFoundException fnfe) {
        throw new DecoderException("Enable to create output file : " + fnfe.getMessage());
    } catch (IOException ioe) {
        throw new DecoderException("Enable to read input file : " + ioe.getMessage());
    }
}