
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.util.Random;

import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZ;
import org.tukaani.xz.XZInputStream;
import org.tukaani.xz.XZOutputStream;

public class CorruptedInputExceptionOnMODE_UNCOMPRESSED {
	public static void main(String[] args) throws Exception {
		final LZMA2Options lzma2 = new LZMA2Options();
		lzma2.setMode(LZMA2Options.MODE_UNCOMPRESSED);
		final byte[] data = new byte[1024 * 1024 * 100];
		new Random().nextBytes(data);
		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
		final XZOutputStream xzos = new XZOutputStream(bos, lzma2, XZ.CHECK_SHA256);
		xzos.write(data);
		xzos.close();
		final XZInputStream xzis = new XZInputStream(new ByteArrayInputStream(bos.toByteArray()));
		if (true) {
			//trigger CorruptedInputException
			while(xzis.read()!=-1) {				
			}
		} else {
			//avoid CorruptedInputException because we do not read/verify hash
			final DataInputStream dis = new DataInputStream(xzis);
			final byte[] verify = new byte[data.length];
			dis.readFully(verify);
			for (int i = 0; i < verify.length; i++) {
				if (data[i] != verify[i]) {
					throw new Exception("difference at index " + i);
				}
			}
		}
	}
}
