class UPS extends Patcher {

    public static UpsCrc readUpsCrc(Context context, File f) throws PatchException, IOException {
        BufferedInputStream stream = null;
        try {
            stream = new BufferedInputStream(new FileInputStream(f));
            CRC32 crc = new CRC32();
            int x;
            for (long i = f.length() - 12; i != 0; i--) {
                x = stream.read();
                if (x == -1)
                    throw new PatchException(context.getString(R.string.notify_error_patch_corrupted));
                crc.update(x);
            }

            long inputCrc = 0;
            for (int i = 0; i < 4; i++) {
                x = stream.read();
                if (x == -1)
                    throw new PatchException(context.getString(R.string.notify_error_patch_corrupted));
                crc.update(x);
                inputCrc += ((long) x) << (i * 8);
            }

            long outputCrc = 0;
            for (int i = 0; i < 4; i++) {
                x = stream.read();
                if (x == -1)
                    throw new PatchException(context.getString(R.string.notify_error_patch_corrupted));
                crc.update(x);
                outputCrc += ((long) x) << (i * 8);
            }

            long realPatchCrc = crc.getValue();
            long patchCrc = readLong(stream);
            if (patchCrc == -1)
                throw new PatchException(context.getString(R.string.notify_error_patch_corrupted));
            return new UpsCrc(inputCrc, outputCrc, patchCrc, realPatchCrc);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

}

class UPSTest {

    private static final String PATCH_CORRUPTED;
    @Rule
    public TemporaryFolder folder;
    @Mock Context mockContext;

    @Test
    public void testReadUpsCrc() throws Exception {

        File patch = new File(this.getClass().getResource("/ups/readUpsCrc.ups").getPath());
        UPS.UpsCrc pCrc = null;
        try {
            pCrc = UPS.readUpsCrc(mockContext, patch);
        } catch (PatchException e) {
            fail("Patch exception");
        }
        assertEquals(pCrc.getPatchFileCRC(), pCrc.getRealPatchCRC());
    }

}