Java Code Examples for java.util.zip.CheckedInputStream#read()

The following examples show how to use java.util.zip.CheckedInputStream#read() . 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: JaveleonModuleReloader.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private long calculateChecksum(URL layer) {
    if (layer == null) {
        return -1;
    }
    try {
        InputStream is = layer.openStream();
        try {
            CheckedInputStream cis = new CheckedInputStream(is, new CRC32());
            // Compute the CRC32 checksum
            byte[] buf = new byte[1024];
            while (cis.read(buf) >= 0) {
            }
            cis.close();
            return cis.getChecksum().getValue();
        } finally {
            is.close();
        }
    } catch (IOException e) {
        return -1;
    }
}
 
Example 2
Source File: Utils.java    From SecuritySample with Apache License 2.0 6 votes vote down vote up
private static long getApkFileChecksum(Context context) {
    String apkPath = context.getPackageCodePath();
    Long chksum = null;
    try {
        // Open the file and build a CRC32 checksum.
        FileInputStream fis = new FileInputStream(new File(apkPath));
        CRC32 chk = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(fis, chk);
        byte[] buff = new byte[80];
        while (cis.read(buff) >= 0) ;
        chksum = chk.getValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chksum;
}
 
Example 3
Source File: CheckedInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_read() throws Exception {
    // testing that the return by skip is valid
    InputStream checkInput = Support_Resources
            .getStream("hyts_checkInput.txt");
    CheckedInputStream checkIn = new CheckedInputStream(checkInput,
            new CRC32());
    checkIn.read();
    checkIn.close();
    try {
        checkIn.read();
        fail("IOException expected.");
    } catch (IOException ee) {
        // expected
    }
    checkInput.close();
}
 
Example 4
Source File: CheckedInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_read$byteII() throws Exception {
    // testing that the return by skip is valid
    InputStream checkInput = Support_Resources
            .getStream("hyts_checkInput.txt");
    CheckedInputStream checkIn = new CheckedInputStream(checkInput,
            new CRC32());
    byte buff[] = new byte[50];
    checkIn.read(buff, 10, 5);
    checkIn.close();
    try {
        checkIn.read(buff, 10, 5);
        fail("IOException expected.");
    } catch (IOException ee) {
        // expected
    }
    checkInput.close();
}
 
Example 5
Source File: URLMonitor.java    From rice with Educational Community License v2.0 6 votes vote down vote up
private Long getCRC(URL zipUrl) {
    Long result = -1l;
    try {
        CRC32 crc = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);

        byte[] buffer = new byte[1024];
        int length;

        //read the entry from zip file and extract it to disk
        while( (length = cis.read(buffer)) > 0);

        cis.close();

        result = crc.getValue();
    } catch (IOException e) {
        LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
    }
    return result;
}
 
Example 6
Source File: AdlerChecksum.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Long getFileChecksum(File file) {
    
    long checksum = -1;
    byte[] buffer = new byte[512];

    try {
        FileInputStream fis = new FileInputStream(file);
        CheckedInputStream cis = new CheckedInputStream(fis, check);
        
        int read = cis.read(buffer);
        while(read != -1){
            read = cis.read(buffer);
        }
        
        fis.close();
        cis.close();

        checksum = check.getValue();
    } catch (IOException ex) {
        logger.error("Error getting file checksum: "+ex);
    }
    return checksum;
}
 
Example 7
Source File: Sha1Checksum.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Long getFileChecksum(File file) {
    long checksum = -1;
    byte[] buffer = new byte[512];
    Checksum check = new Adler32();

    try {
        FileInputStream fis = new FileInputStream(file);
        CheckedInputStream cis = new CheckedInputStream(fis, check);
        
        int read = cis.read(buffer);
        while(read != -1){
            read = cis.read(buffer);
        }

        checksum = check.getValue();
    } catch (IOException ex) {
        logger.error("Error getting file checksum: "+ex);
    }
    return checksum;
}
 
Example 8
Source File: MimePackage.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Return the CRC checksum of a given part
 *
 * @param partIndex
 *            the index of the part
 * @param isAttachment
 *            true if the part is an attachment
 * @return the part checksum
 * @throws PackageException
 */
@PublicAtsApi
public long getPartChecksum(
                             int partIndex,
                             boolean isAttachment ) throws PackageException {

    InputStream partDataStream = getPartData(partIndex, isAttachment);

    if (partDataStream != null) {
        try {
            SeekInputStream seekDataStream = new SeekInputStream(partDataStream);
            seekDataStream.seek(0);

            // create a new crc and reset it
            CRC32 crc = new CRC32();

            // use checked stream to get the checksum
            CheckedInputStream stream = new CheckedInputStream(seekDataStream, crc);

            int bufLen = 4096;
            byte[] buffer = new byte[bufLen];
            int numBytesRead = bufLen;

            while (numBytesRead == bufLen) {
                numBytesRead = stream.read(buffer, 0, bufLen);
            }

            long checksum = stream.getChecksum().getValue();
            stream.close();

            return checksum;
        } catch (IOException ioe) {
            throw new PackageException(ioe);
        }
    } else {
        throw new MimePartWithoutContentException("MIME part does not have any content");
    }
}
 
Example 9
Source File: PerformanceTest.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 5 votes vote down vote up
static void createTestFile() throws Throwable {

		/**
		 * Generate a temporary file for uploading/downloading
		 */
		sourceFile = new File(System.getProperty("user.home"), "sftp-file");
		java.util.Random rnd = new java.util.Random();

		FileOutputStream out = new FileOutputStream(sourceFile);
		byte[] buf = new byte[1024000];
		for (int i = 0; i < 100; i++) {
			rnd.nextBytes(buf);
			out.write(buf);
		}
		out.close();

		CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
				sourceFile), new Adler32());

		try {
			byte[] tempBuf = new byte[16384];
			while (cis.read(tempBuf) >= 0) {
			}
			sourceFileChecksum = cis.getChecksum().getValue();
		} catch (IOException e) {
		} finally {
			cis.close();
		}
	}
 
Example 10
Source File: CheckedInputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.zip.CheckedInputStream#getChecksum()
 */
public void test_getChecksum() throws Exception {
    byte outBuf[] = new byte[100];
    File f = File.createTempFile("CheckedInputStreamTest", ".txt");
    InputStream inEmp = new FileInputStream(f);
    CheckedInputStream checkEmpty = new CheckedInputStream(inEmp, new CRC32());
    while (checkEmpty.read() >= 0) {
    }
    assertEquals("the checkSum value of an empty file is not zero", 0, checkEmpty
            .getChecksum().getValue());
    inEmp.close();

    // testing getChecksum for the file checkInput
    InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt");
    CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32());
    while (checkIn.read() >= 0) {
    }
    // ran JDK and found that the checkSum value of this is 2036203193
    // System.out.print(" " + checkIn.getChecksum().getValue());
    assertEquals("the checksum value is incorrect", 2036203193, checkIn.getChecksum()
            .getValue());
    checkInput.close();
    // testing getChecksum for file checkInput
    checkInput = Support_Resources.getStream("hyts_checkInput.txt");
    CheckedInputStream checkIn2 = new CheckedInputStream(checkInput, new CRC32());
    checkIn2.read(outBuf, 0, 10);
    // ran JDK and found that the checkSum value of this is 2235765342
    // System.out.print(" " + checkIn2.getChecksum().getValue());
    assertEquals("the checksum value is incorrect", 2235765342L, checkIn2.getChecksum()
            .getValue());
    checkInput.close();
}
 
Example 11
Source File: OS.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public static long getAlder32(File f) {
	try {
		FileInputStream inputStream = new FileInputStream(f);
		Adler32 adlerChecksum = new Adler32();
		CheckedInputStream cinStream = new CheckedInputStream(inputStream, adlerChecksum);
		byte[] b = new byte[128];
		while (cinStream.read(b) >= 0) {
		}
		long checksum = cinStream.getChecksum().getValue();
		cinStream.close();
		return checksum;
	} catch (IOException e) {
		return 0;
	}
}
 
Example 12
Source File: MondrianFileSchemaModel.java    From pentaho-aggdesigner with GNU General Public License v2.0 5 votes vote down vote up
public long recalculateSchemaChecksum() {
  if (getMondrianSchemaFilename() != null) {
    try {
      CheckedInputStream cis = new CheckedInputStream(new FileInputStream(getMondrianSchemaFilename()), new CRC32());
      byte[] buf = new byte[1024];
      while(cis.read(buf) >= 0) {
      }
      return cis.getChecksum().getValue();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
  return -1;
}
 
Example 13
Source File: PerformanceTest.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 4 votes vote down vote up
static void performTest(SshClient ssh) throws Throwable {

		/**
		 * Authenticate the user using password authentication
		 */
		PasswordAuthentication pwd = new PasswordAuthentication();
		pwd.setPassword(password);

		ssh.authenticate(pwd);

		/**
		 * Start a session and do basic IO
		 */
		if (ssh.isAuthenticated()) {

			System.out.println("Client info: " + ssh.toString());
			SftpClient sftp = new SftpClient(ssh);
			sftp.setBlockSize(32768);
			sftp.setTransferMode(SftpClient.MODE_BINARY);

			/**
			 * Create a directory for the test files
			 */
			sftp.mkdirs("sftp/test-files");

			/**
			 * Change directory
			 */
			sftp.cd("sftp/test-files");

			/**
			 * Put a file into our new directory
			 */
			long length = sourceFile.length();
			long t1 = System.currentTimeMillis();
			sftp.put(sourceFile.getAbsolutePath());
			long t2 = System.currentTimeMillis();
			long e = t2 - t1;
			float kbs;
			if (e >= 1000) {
				kbs = (((float) length / 1024) / ((float) e / 1000) / 1000);
				System.out.println("Upload Transfered at " + df.format(kbs)
						+ " MB/s");
			} else {
				System.out.println("Download transfered in under one second");
			}

			/**
			 * Download the file inot a new location
			 */
			File f2 = new File(System.getProperty("user.home"), "downloaded");
			f2.mkdir();

			sftp.lcd(f2.getAbsolutePath());
			File retrievedFile = new File(f2, sourceFile.getName());
			t1 = System.currentTimeMillis();
			sftp.get(sourceFile.getName());
			t2 = System.currentTimeMillis();
			e = t2 - t1;
			if (e >= 1000) {
				kbs = (((float) length / 1024) / ((float) e / 1000) / 1000);
				System.out.println("Download Transfered at " + df.format(kbs)
						+ " MB/s");
			} else {
				System.out.println("Download transfered in under one second");
			}

			long checksum2 = 0;
			CheckedInputStream cis = new CheckedInputStream(
					new FileInputStream(retrievedFile), new Adler32());
			try {
				byte[] tempBuf = new byte[16384];
				while (cis.read(tempBuf) >= 0) {
				}
				checksum2 = cis.getChecksum().getValue();
			} catch (IOException ex) {
			} finally {
				cis.close();
			}

			if (checksum2 != sourceFileChecksum) {
				System.out.println("FILES DO NOT MATCH");
			}
		}
	}
 
Example 14
Source File: ChecksumUtils.java    From tutorials with MIT License 4 votes vote down vote up
public static long getChecksumCRC32(InputStream stream, int bufferSize) throws IOException {
    CheckedInputStream checkedInputStream = new CheckedInputStream(stream, new CRC32());
    byte[] buffer = new byte[bufferSize];
    while (checkedInputStream.read(buffer, 0, buffer.length) >= 0) {}
    return checkedInputStream.getChecksum().getValue();
}