Java Code Examples for java.util.zip.CRC32

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project engine, under directory /main/com/midtro/platform/modules/assets/task/.

Source file: UpdateLoadTask.java

  24 
vote

/** 
 * Calculates the CRC32 of a file.
 * @param file The file.
 * @return The CRC32 checksum.
 * @throws IOException If an IO error occurs.
 */
private static long getCRC(final File file) throws IOException {
  final FileInputStream fis=new FileInputStream(file);
  final byte[] data=new byte[(int)file.length()];
  fis.read(data);
  fis.close();
  final CRC32 crc32=new CRC32();
  crc32.update(data);
  return crc32.getValue();
}
 

Example 2

From project commons-compress, under directory /src/main/java/org/apache/commons/compress/archivers/zip/.

Source file: AbstractUnicodeExtraField.java

  22 
vote

/** 
 * Assemble as unicode extension from the name/comment and encoding of the original zip entry.
 * @param text The file name or comment.
 * @param bytes The encoded of the filename or comment in the zipfile.
 * @param off The offset of the encoded filename or comment in<code>bytes</code>.
 * @param len The length of the encoded filename or commentin<code>bytes</code>.
 */
protected AbstractUnicodeExtraField(String text,byte[] bytes,int off,int len){
  CRC32 crc32=new CRC32();
  crc32.update(bytes,off,len);
  nameCRC32=crc32.getValue();
  try {
    unicodeName=text.getBytes(CharsetNames.UTF_8);
  }
 catch (  UnsupportedEncodingException e) {
    throw new RuntimeException("FATAL: UTF-8 encoding not supported.",e);
  }
}
 

Example 3

From project erjang, under directory /src/main/java/erjang/.

Source file: EBinary.java

  22 
vote

public long crc(){
  CRC32 crc=new CRC32();
  int octets=byteSize();
  crc.update(data,byteOffset(),octets);
  return crc.getValue();
}
 

Example 4

From project httpClient, under directory /httpclient/src/test/java/org/apache/http/client/entity/.

Source file: TestDecompressingEntity.java

  22 
vote

@Test public void testNonStreaming() throws Exception {
  CRC32 crc32=new CRC32();
  StringEntity wrapped=new StringEntity("1234567890","ASCII");
  ChecksumEntity entity=new ChecksumEntity(wrapped,crc32);
  Assert.assertFalse(entity.isStreaming());
  String s=EntityUtils.toString(entity);
  Assert.assertEquals("1234567890",s);
  Assert.assertEquals(639479525L,crc32.getValue());
  InputStream in1=entity.getContent();
  InputStream in2=entity.getContent();
  Assert.assertTrue(in1 != in2);
}
 

Example 5

From project JitCask, under directory /src/com/afewmoreamps/.

Source file: MiniCask.java

  22 
vote

public static byte[] constructTombstoneEntry(byte keyHash[],int fileId,long timestamp){
  ByteBuffer entryBuf=ByteBuffer.allocate(8 + 20 + 4+ 8);
  entryBuf.position(4);
  entryBuf.putInt(-1);
  entryBuf.put(keyHash);
  entryBuf.putLong(timestamp);
  final CRC32 crc=new CRC32();
  crc.update(entryBuf.array(),4,entryBuf.capacity() - 4);
  entryBuf.putInt(0,(int)crc.getValue());
  return entryBuf.array();
}
 

Example 6

From project jnrpe-lib, under directory /jnrpe-lib/src/main/java/it/jnrpe/net/.

Source file: JNRPEResponse.java

  22 
vote

/** 
 * Updates the CRC value.
 */
public void updateCRC(){
  setCRC(0);
  int iCRC=0;
  CRC32 crcAlg=new CRC32();
  crcAlg.update(toByteArray());
  iCRC=(int)crcAlg.getValue();
  setCRC(iCRC);
}
 

Example 7

From project MIT-Mobile-for-Android, under directory /src/edu/mit/mitmobile2/about/.

Source file: AboutActivity.java

  22 
vote

private void writeChunk(FileOutputStream stream,String header,byte[] content) throws IOException {
  stream.write(splitInt(content.length));
  int len=header.length() + content.length;
  ByteBuffer buf=ByteBuffer.allocate(len);
  buf.put(new byte[]{(byte)header.charAt(0),(byte)header.charAt(1),(byte)header.charAt(2),(byte)header.charAt(3)});
  buf.put(content);
  CRC32 crc32=new CRC32();
  crc32.update(buf.array(),0,len);
  stream.write(buf.array());
  stream.write(splitInt(crc32.getValue()));
}
 

Example 8

From project mylyn.docs, under directory /org.eclipse.mylyn.docs.epub.core/src/org/eclipse/mylyn/internal/docs/epub/core/.

Source file: EPUBFileUtil.java

  22 
vote

/** 
 * A correctly formatted EPUB file must contain an uncompressed entry named <b>mimetype</b> that is placed at the beginning. The contents of this file must be the ASCII-encoded string <b>application/epub+zip</b>. This method will create this file.
 * @param zos the zip output stream to write to.
 * @throws IOException
 */
public static void writeEPUBHeader(ZipOutputStream zos) throws IOException {
  byte[] bytes=EPUB.MIMETYPE_EPUB.getBytes("ASCII");
  ZipEntry mimetype=new ZipEntry("mimetype");
  mimetype.setMethod(ZipOutputStream.STORED);
  mimetype.setSize(bytes.length);
  mimetype.setCompressedSize(bytes.length);
  CRC32 crc=new CRC32();
  crc.update(bytes);
  mimetype.setCrc(crc.getValue());
  zos.putNextEntry(mimetype);
  zos.write(bytes);
  zos.closeEntry();
}
 

Example 9

From project nevernote, under directory /src/cx/fbn/nevernote/evernote/.

Source file: EnCrypt.java

  22 
vote

private String crcHeader(byte[] bytes){
  CRC32 crc=new CRC32();
  crc.update(bytes);
  int realCRC=(int)crc.getValue();
  realCRC=realCRC ^ (-1);
  realCRC=realCRC >>> 0;
  String hexCRC=Integer.toHexString(realCRC).substring(0,4);
  return hexCRC.toString().toUpperCase();
}
 

Example 10

From project Orebfuscator, under directory /src/com/lishid/orebfuscator/obfuscation/.

Source file: CalculationsUtil.java

  22 
vote

public static long Hash(byte[] data,int length){
  CRC32 crc=new CRC32();
  crc.reset();
  crc.update(data,0,length);
  long hash=crc.getValue();
  return hash;
}
 

Example 11

From project org.openscada.external, under directory /org.openscada.external.jOpenDocument/src/org/jopendocument/util/.

Source file: Zip.java

  22 
vote

/** 
 * Zip the passed array with the  {@link ZipEntry#STORED} method. This method takes care of theCRC and size.
 * @param name the entry name.
 * @param in what to zip.
 * @throws IOException if an error occurs.
 */
public synchronized void zipNonCompressed(String name,byte[] in) throws IOException {
  final ZipEntry entry=new ZipEntry(name);
  entry.setMethod(ZipEntry.STORED);
  final CRC32 crc=new CRC32();
  crc.update(in);
  entry.setCrc(crc.getValue());
  entry.setSize(in.length);
  this.putNextEntry(entry);
  this.getOutStream().write(in);
  this.closeEntry();
}
 

Example 12

From project parboiled, under directory /parboiled-java/src/test/java/org/parboiled/transform/.

Source file: InstructionGroupCreatorTest.java

  22 
vote

private static long computeCRC(String text) throws Exception {
  CRC32 crc32=new CRC32();
  byte[] buf=text.getBytes("UTF8");
  crc32.update(buf);
  return crc32.getValue();
}
 

Example 13

From project QuakeInjector, under directory /src/de/haukerehfeld/quakeinjector/.

Source file: Utils.java

  22 
vote

public static long copy(final InputStream in,final OutputStream out,final int BUFFERSIZE,final ProgressListener progress) throws IOException {
  byte data[]=new byte[BUFFERSIZE];
  CRC32 crc=new CRC32();
  int readcount;
  while ((readcount=in.read(data,0,BUFFERSIZE)) != -1) {
    if (progress != null) {
      progress.publish(readcount);
    }
    out.write(data,0,readcount);
    crc.update(data,0,readcount);
  }
  out.flush();
  return crc.getValue();
}
 

Example 14

From project SimpleSpleef, under directory /src/main/java/de/beimax/simplespleef/util/.

Source file: ConfigHelper.java

  22 
vote

/** 
 * Calculate crc of input stream - stream is read, but not closed!
 * @param is
 * @return crc32 sum of stream
 * @throws Exception
 */
protected long crc32Sum(InputStream is) throws Exception {
  CRC32 crc=new CRC32();
  byte[] ba=new byte[(int)is.available()];
  is.read(ba);
  crc.update(ba);
  is.close();
  return crc.getValue();
}
 

Example 15

From project TextSecure, under directory /src/org/thoughtcrime/securesms/mms/.

Source file: PngTransport.java

  22 
vote

private byte[] calculateChunkCrc(byte[] header,byte[] chunk){
  byte[] crcBytes=new byte[4];
  CRC32 crc=new CRC32();
  crc.update(header,4,header.length - 4);
  if (chunk != null)   crc.update(chunk,0,chunk.length);
  long crcValue=crc.getValue();
  Conversions.longTo4ByteArray(crcBytes,0,crcValue);
  return crcBytes;
}
 

Example 16

From project xmlcalabash1, under directory /src/com/xmlcalabash/library/.

Source file: Hash.java

  22 
vote

private String crc(String value,String version){
  if (version == null) {
    version="32";
  }
  if (!"32".equals(version)) {
    throw XProcException.dynamicError(36);
  }
  CRC32 crc=new CRC32();
  crc.update(value.getBytes());
  return Long.toHexString(crc.getValue());
}
 

Example 17

From project archive-commons, under directory /archive-commons/src/main/java/org/archive/format/gzip/.

Source file: GZIPSeriesMember.java

  21 
vote

public GZIPSeriesMember(GZIPMemberSeries series,GZIPHeader header){
  this.series=series;
  this.header=header;
  this.footer=null;
  this.inflater=new Inflater(true);
  this.crc=new CRC32();
  gotIOError=false;
  gotGZError=false;
  gotEOR=false;
}
 

Example 18

From project clustermeister, under directory /provisioning/src/main/java/com/github/nethad/clustermeister/provisioning/utils/.

Source file: FileUtils.java

  21 
vote

/** 
 * Compute Cyclic Redundancy Check (CRC32).
 * @param in the InputStream to compute
 * @return CRC32 for the given InputStream
 * @throws IOException 
 */
public static synchronized long getCRC32(final InputStream in) throws IOException {
  return ByteStreams.getChecksum(new InputSupplier<InputStream>(){
    @Override public InputStream getInput() throws IOException {
      return in;
    }
  }
,new CRC32());
}
 

Example 19

From project commons-io, under directory /src/test/java/org/apache/commons/io/.

Source file: FileUtilsTestCase.java

  21 
vote

public void testChecksumCRC32() throws Exception {
  String text="Imagination is more important than knowledge - Einstein";
  File file=new File(getTestDirectory(),"checksum-test.txt");
  FileUtils.writeStringToFile(file,text,"US-ASCII");
  Checksum expectedChecksum=new CRC32();
  expectedChecksum.update(text.getBytes("US-ASCII"),0,text.length());
  long expectedValue=expectedChecksum.getValue();
  long resultValue=FileUtils.checksumCRC32(file);
  assertEquals(expectedValue,resultValue);
}
 

Example 20

From project DirectMemory, under directory /DirectMemory-Cache/src/test/java/org/apache/directmemory/memory/.

Source file: BaseTest.java

  21 
vote

public long crc(String str){
  final Checksum checksum=new CRC32();
  final byte bytes[]=str.getBytes();
  checksum.update(bytes,0,bytes.length);
  return checksum.getValue();
}
 

Example 21

From project echo2, under directory /src/webcontainer/java/nextapp/echo2/webcontainer/image/.

Source file: PngEncoder.java

  21 
vote

/** 
 * Encodes the image.
 * @param out an OutputStream to which the encoded image will bewritten
 * @throws IOException if a problem is encountered writing the output
 */
public synchronized void encode(OutputStream out) throws IOException {
  Checksum csum=new CRC32();
  out=new CheckedOutputStream(out,csum);
  out.write(SIGNATURE);
  writeIhdrChunk(out,csum);
  if (outputBpp == 1) {
    writePlteChunk(out,csum);
  }
  writeIdatChunks(out,csum);
  writeIendChunk(out,csum);
}
 

Example 22

From project echo3, under directory /src/server-java/webcontainer/nextapp/echo/webcontainer/util/.

Source file: PngEncoder.java

  21 
vote

/** 
 * Encodes the image.
 * @param out an OutputStream to which the encoded image will bewritten
 * @throws IOException if a problem is encountered writing the output
 */
public synchronized void encode(OutputStream out) throws IOException {
  Checksum csum=new CRC32();
  out=new CheckedOutputStream(out,csum);
  out.write(SIGNATURE);
  writeIhdrChunk(out,csum);
  if (outputBpp == 1) {
    writePlteChunk(out,csum);
  }
  writeIdatChunks(out,csum);
  writeIendChunk(out,csum);
}
 

Example 23

From project iGeo, under directory /io/.

Source file: IRhino3dm.java

  21 
vote

public Chunk(int h,int b,byte[] c){
  header=h;
  body=b;
  content=c;
  if (doCRC()) {
    crc=new CRC32();
    crc.update(content);
  }
}
 

Example 24

From project indextank-engine, under directory /flaptor-util/com/flaptor/util/.

Source file: FileUtil.java

  21 
vote

/** 
 * Returns the checksum of a given directory tree. 
 * @throws java.io.IOException
 */
public static long computeChecksum(String path) throws IOException {
  String dir=getDir(path);
  Checksum checksum=new CRC32();
  checksumDir(checksum,dir);
  return checksum.getValue();
}
 

Example 25

From project JGit, under directory /org.spearce.jgit/src/org/spearce/jgit/lib/.

Source file: PackFile.java

  21 
vote

final void copyRawData(final PackedObjectLoader loader,final OutputStream out,final byte buf[]) throws IOException {
  final long objectOffset=loader.objectOffset;
  final long dataOffset=loader.dataOffset;
  final int cnt=(int)(findEndOffset(objectOffset) - dataOffset);
  final WindowCursor curs=loader.curs;
  final PackIndex idx=idx();
  if (idx.hasCRC32Support()) {
    final CRC32 crc=new CRC32();
    int headerCnt=(int)(dataOffset - objectOffset);
    while (headerCnt > 0) {
      int toRead=Math.min(headerCnt,buf.length);
      int read=pack.read(objectOffset,buf,0,toRead,curs);
      if (read != toRead)       throw new EOFException();
      crc.update(buf,0,read);
      headerCnt-=toRead;
    }
    final CheckedOutputStream crcOut=new CheckedOutputStream(out,crc);
    pack.copyToStream(dataOffset,buf,cnt,crcOut,curs);
    final long computed=crc.getValue();
    final ObjectId id=findObjectForOffset(objectOffset);
    final long expected=idx.findCRC32(id);
    if (computed != expected)     throw new CorruptObjectException("Object at " + dataOffset + " in "+ getPackFile()+ " has bad zlib stream");
  }
 else {
    try {
      pack.verifyCompressed(dataOffset,curs);
    }
 catch (    DataFormatException dfe) {
      final CorruptObjectException coe;
      coe=new CorruptObjectException("Object at " + dataOffset + " in "+ getPackFile()+ " has bad zlib stream");
      coe.initCause(dfe);
      throw coe;
    }
    pack.copyToStream(dataOffset,buf,cnt,out,curs);
  }
}
 

Example 26

From project kevoree-library, under directory /javase/org.kevoree.library.javase.webserver.aceEditor/src/main/java/com/google/gwt/user/server/rpc/impl/.

Source file: SerializabilityUtil.java

  21 
vote

public static String getSerializationSignature(Class<?> instanceType,SerializationPolicy policy){
  String result;
synchronized (classCRC32Cache) {
    result=classCRC32Cache.get(instanceType);
    if (result == null) {
      CRC32 crc=new CRC32();
      try {
        generateSerializationSignature(instanceType,crc,policy);
      }
 catch (      UnsupportedEncodingException e) {
        throw new RuntimeException("Could not compute the serialization signature",e);
      }
      result=Long.toString(crc.getValue());
      classCRC32Cache.put(instanceType,result);
    }
  }
  return result;
}
 

Example 27

From project nuxeo-platform-rendering-templates, under directory /src/main/java/org/nuxeo/ecm/platform/template/odt/.

Source file: OOoArchiveModifier.java

  21 
vote

protected void writeOOoEntry(ZipOutputStream zipOutputStream,String entryName,File fileEntry,int zipMethod) throws Exception {
  if (fileEntry.isDirectory()) {
    entryName=entryName + "/";
    ZipEntry zentry=new ZipEntry(entryName);
    zipOutputStream.putNextEntry(zentry);
    zipOutputStream.closeEntry();
    for (    File child : fileEntry.listFiles()) {
      writeOOoEntry(zipOutputStream,entryName + child.getName(),child,zipMethod);
    }
    return;
  }
  ZipEntry zipEntry=new ZipEntry(entryName);
  InputStream entryInputStream=new FileInputStream(fileEntry);
  zipEntry.setMethod(zipMethod);
  if (zipMethod == ZipEntry.STORED) {
    byte[] inputBytes=FileUtils.readBytes(entryInputStream);
    CRC32 crc=new CRC32();
    crc.update(inputBytes);
    zipEntry.setCrc(crc.getValue());
    zipEntry.setSize(inputBytes.length);
    zipEntry.setCompressedSize(inputBytes.length);
    zipOutputStream.putNextEntry(zipEntry);
    FileUtils.copy(new ByteArrayInputStream(inputBytes),zipOutputStream);
  }
 else {
    zipOutputStream.putNextEntry(zipEntry);
    FileUtils.copy(entryInputStream,zipOutputStream);
  }
  try {
    entryInputStream.close();
  }
 catch (  IOException e) {
  }
  zipOutputStream.closeEntry();
}
 

Example 28

From project platform_external_jsilver, under directory /src/com/google/clearsilver/jsilver/functions/string/.

Source file: CrcFunction.java

  21 
vote

/** 
 * @param args 1 string expression
 * @return CRC-32 of string as number value
 */
public Value execute(Value... args){
  String string=args[0].asString();
  Checksum crc=new CRC32();
  byte[] b;
  try {
    b=string.getBytes("UTF-8");
  }
 catch (  UnsupportedEncodingException e) {
    throw new AssertionError("UTF-8 must be supported");
  }
  crc.update(b,0,b.length);
  return literalConstant((int)crc.getValue(),args[0]);
}
 

Example 29

From project platform_packages_apps_browser, under directory /src/com/android/browser/.

Source file: BrowserBackupAgent.java

  21 
vote

private long copyBackupToFile(BackupDataInput data,File file,int toRead) throws IOException {
  final int CHUNK=8192;
  byte[] buf=new byte[CHUNK];
  CRC32 crc=new CRC32();
  FileOutputStream out=new FileOutputStream(file);
  try {
    while (toRead > 0) {
      int numRead=data.readEntityData(buf,0,CHUNK);
      crc.update(buf,0,numRead);
      out.write(buf,0,numRead);
      toRead-=numRead;
    }
  }
  finally {
    if (out != null) {
      out.close();
    }
  }
  return crc.getValue();
}
 

Example 30

From project plexus-archiver, under directory /src/main/java/org/codehaus/plexus/archiver/zip/.

Source file: AbstractZipArchiver.java

  21 
vote

private void readWithZipStats(InputStream in,byte[] header,ZipEntry ze,ByteArrayOutputStream bos) throws IOException {
  byte[] buffer=new byte[8 * 1024];
  CRC32 cal2=new CRC32();
  long size=0;
  for (  byte aHeader : header) {
    cal2.update(aHeader);
    size++;
  }
  int count=0;
  do {
    size+=count;
    cal2.update(buffer,0,count);
    if (bos != null) {
      bos.write(buffer,0,count);
    }
    count=in.read(buffer,0,buffer.length);
  }
 while (count != -1);
  ze.setSize(size);
  ze.setCrc(cal2.getValue());
}
 

Example 31

From project radrails, under directory /plugins/com.aptana.rdt.profiling/src/com/aptana/rdt/profiling/.

Source file: ClientKey.java

  21 
vote

/** 
 * Decrypts an encrypted string
 * @param encrypted
 * @return - decrypted string
 */
public String decrypt(String encrypted){
  try {
    if (encrypted == null) {
      encrypted="";
    }
 else {
      encrypted=trimEncryptedLicense(encrypted);
    }
    BigInteger big=new BigInteger(encrypted);
    BigInteger decrypted=big.modPow(exponent,modulus);
    long crc32Value=((long)decrypted.intValue()) & 0xffffffffL;
    decrypted=decrypted.shiftRight(32);
    byte[] bytes=decrypted.toByteArray();
    CRC32 crc32=new CRC32();
    crc32.update(bytes);
    if (crc32Value == crc32.getValue()) {
      return new String(bytes);
    }
  }
 catch (  NumberFormatException e) {
  }
  return null;
}
 

Example 32

From project scufl2, under directory /scufl2-ucfpackage/src/main/java/uk/org/taverna/scufl2/ucfpackage/impl/odfdom/pkg/.

Source file: OdfPackage.java

  21 
vote

/** 
 * Data was updated, update mZipEntry and OdfFileEntry as well
 */
private void entryUpdate(String packagePath) throws Exception, SAXException, TransformerConfigurationException, TransformerException, ParserConfigurationException {
  byte[] data=getBytes(packagePath);
  int size=0;
  if (data == null) {
    size=0;
  }
 else {
    size=data.length;
  }
  OdfFileEntry fileEntry=getManifestEntries().get(packagePath);
  ZipEntry zipEntry=mZipEntries.get(packagePath);
  if (fileEntry != null) {
    fileEntry.setSize(size);
  }
  if (zipEntry == null) {
    return;
  }
  zipEntry.setSize(size);
  CRC32 crc=new CRC32();
  if ((data != null) && size > 0) {
    crc.update(data);
  }
  zipEntry.setCrc(crc.getValue());
  zipEntry.setCompressedSize(-1);
  long modTime=(new java.util.Date()).getTime();
  zipEntry.setTime(modTime);
}
 

Example 33

From project showcase, under directory /src/main/java/org/richfaces/demo/mediaOutput/.

Source file: MediaBean.java

  21 
vote

@Override protected void writeSectionData(InputStream inChannel,OutputStream outChannel) throws IOException {
  if (!isIndexed) {
    super.writeSectionData(inChannel,outChannel);
  }
 else {
    byte[] data=new byte[2000 * 3];
    int read;
    int remaining=sectionLength;
    assert(data.length < BUFFER_SIZE);
    CRC32 crc32=new CRC32();
    crc32.update(chunkTypeBytes);
    while ((read=(inChannel.read(data,0,Math.min(remaining,data.length)))) > 0) {
      remaining-=read;
      transformColors(data,0,read);
      outChannel.write(data,0,read);
      crc32.update(data,0,read);
    }
    if (remaining > 0) {
      throw new IllegalArgumentException();
    }
    inChannel.skip(4);
    writeInt(outChannel,(int)crc32.getValue());
  }
}
 

Example 34

From project subsonic, under directory /subsonic-main/src/main/java/net/sourceforge/subsonic/controller/.

Source file: DownloadController.java

  21 
vote

/** 
 * Computes the CRC checksum for the given file.
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum.
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
  CRC32 crc=new CRC32();
  InputStream in=new FileInputStream(file);
  try {
    byte[] buf=new byte[8192];
    int n=in.read(buf);
    while (n != -1) {
      crc.update(buf,0,n);
      n=in.read(buf);
    }
  }
  finally {
    in.close();
  }
  return crc.getValue();
}
 

Example 35

From project subsonic_1, under directory /subsonic-main/src/main/java/net/sourceforge/subsonic/controller/.

Source file: DownloadController.java

  21 
vote

/** 
 * Computes the CRC checksum for the given file.
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum.
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
  CRC32 crc=new CRC32();
  InputStream in=new FileInputStream(file);
  try {
    byte[] buf=new byte[8192];
    int n=in.read(buf);
    while (n != -1) {
      crc.update(buf,0,n);
      n=in.read(buf);
    }
  }
  finally {
    in.close();
  }
  return crc.getValue();
}
 

Example 36

From project subsonic_2, under directory /subsonic-main/src/main/java/net/sourceforge/subsonic/controller/.

Source file: DownloadController.java

  21 
vote

/** 
 * Computes the CRC checksum for the given file.
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum.
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
  CRC32 crc=new CRC32();
  InputStream in=new FileInputStream(file);
  try {
    byte[] buf=new byte[8192];
    int n=in.read(buf);
    while (n != -1) {
      crc.update(buf,0,n);
      n=in.read(buf);
    }
  }
  finally {
    in.close();
  }
  return crc.getValue();
}
 

Example 37

From project Supersonic, under directory /subsonic-main/src/main/java/net/sourceforge/subsonic/controller/.

Source file: DownloadController.java

  21 
vote

/** 
 * Computes the CRC checksum for the given file.
 * @param file The file to compute checksum for.
 * @return A CRC32 checksum.
 * @throws IOException If an I/O error occurs.
 */
private long computeCrc(File file) throws IOException {
  CRC32 crc=new CRC32();
  InputStream in=new FileInputStream(file);
  try {
    byte[] buf=new byte[8192];
    int n=in.read(buf);
    while (n != -1) {
      crc.update(buf,0,n);
      n=in.read(buf);
    }
  }
  finally {
    in.close();
  }
  return crc.getValue();
}
 

Example 38

From project teatrove, under directory /teaservlet/src/main/java/org/teatrove/teaservlet/.

Source file: ApplicationResponseImpl.java

  21 
vote

private int computeCRC(ByteData bytes) throws IOException {
  final CRC32 crc=new CRC32();
  OutputStream out=new OutputStream(){
    public void write(    int b){
      crc.update(b);
    }
    public void write(    byte[] b){
      crc.update(b,0,b.length);
    }
    public void write(    byte[] b,    int off,    int len){
      crc.update(b,off,len);
    }
  }
;
  bytes.writeTo(out);
  return (int)crc.getValue();
}
 

Example 39

From project tika, under directory /tika-server/src/main/java/org/apache/tika/server/.

Source file: ZipWriter.java

  21 
vote

private static void zipStoreBuffer(ZipArchiveOutputStream zip,String name,byte[] dataBuffer) throws IOException {
  ZipEntry zipEntry=new ZipEntry(name != null ? name : UUID.randomUUID().toString());
  zipEntry.setMethod(ZipOutputStream.STORED);
  zipEntry.setSize(dataBuffer.length);
  CRC32 crc32=new CRC32();
  crc32.update(dataBuffer);
  zipEntry.setCrc(crc32.getValue());
  try {
    zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
  }
 catch (  ZipException ex) {
    if (name != null) {
      zipStoreBuffer(zip,"x-" + name,dataBuffer);
      return;
    }
  }
  zip.write(dataBuffer);
  zip.closeArchiveEntry();
}
 

Example 40

From project tocollege.net, under directory /src/main/java/com/apress/progwt/server/gwt/.

Source file: SerializabilityUtil_1_5_3.java

  21 
vote

public static String getSerializationSignature(Class<?> instanceType){
  String result=getCachedCRCForClass(instanceType);
  if (result == null) {
    CRC32 crc=new CRC32();
    try {
      generateSerializationSignature(instanceType,crc);
    }
 catch (    UnsupportedEncodingException e) {
      throw new RuntimeException("Could not compute the serialization signature",e);
    }
    result=Long.toString(crc.getValue());
    putCachedCRCForClass(instanceType,result);
  }
  return result;
}
 

Example 41

From project JMaNGOS, under directory /Tools/src/main/java/org/jmangos/tools/openGL/.

Source file: PNGDecoder.java

  20 
vote

public PNGDecoder(final InputStream input) throws IOException {
  this.input=input;
  this.crc=new CRC32();
  this.buffer=new byte[4096];
  readFully(this.buffer,0,SIGNATURE.length);
  if (!checkSignature(this.buffer)) {
    throw new IOException("Not a valid PNG file");
  }
  openChunk(IHDR);
  readIHDR();
  closeChunk();
  searchIDAT:   for (; ; ) {
    openChunk();
switch (this.chunkType) {
case IDAT:
      break searchIDAT;
case PLTE:
    readPLTE();
  break;
case tRNS:
readtRNS();
break;
}
closeChunk();
}
if ((this.colorType == COLOR_INDEXED) && (this.palette == null)) {
throw new IOException("Missing PLTE chunk");
}
}
 

Example 42

From project PIE, under directory /R2/pie-runtime/src/main/java/com/pieframework/runtime/utils/azure/.

Source file: StorageManager.java

  20 
vote

public Long calculateChecksum(File file){
  Long result=0L;
  try {
    Checksum checksum;
    FileInputStream is=new FileInputStream(file);
    byte data[]=new byte[2048];
    while (is.read(data) >= 0) {
    }
    checksum=new CRC32();
    checksum.update(data,0,data.length);
    is.close();
    result=checksum.getValue();
  }
 catch (  FileNotFoundException e) {
    e.printStackTrace();
  }
catch (  IOException e) {
    e.printStackTrace();
  }
  return result;
}
 

Example 43

From project heritrix3, under directory /commons/src/main/java/org/archive/io/.

Source file: GzipHeader.java

  19 
vote

/** 
 * Read a byte.  We do not expect to get a -1 reading.  If we do, we throw exception. Update the crc as we go.
 * @param in InputStream to read.
 * @param crc CRC to update.
 * @return Byte read.
 * @throws IOException
 */
protected int readByte(InputStream in,CRC32 crc) throws IOException {
  int b=in.read();
  if (b == -1) {
    throw new EOFException();
  }
  if (crc != null) {
    crc.update(b);
  }
  return b & 0xff;
}
 

Example 44

From project mkgmap, under directory /src/uk/me/parabola/tdbfmt/.

Source file: TdbFile.java

  19 
vote

public void write(String name) throws IOException {
  CheckedOutputStream stream=new CheckedOutputStream(new BufferedOutputStream(new FileOutputStream(name)),new CRC32());
  if (headerBlock == null || overviewMapBlock == null)   throw new IOException("Attempting to write file without being fully set up");
  try {
    Block block=new Block(BLOCK_HEADER);
    headerBlock.write(block);
    block.write(stream);
    block=new Block(BLOCK_COPYRIGHT);
    copyrightBlock.write(block);
    block.write(stream);
    if (tdbVersion >= TDB_V407) {
      block=new Block(BLOCK_R);
      rblock.write(block);
      block.write(stream);
    }
    block=new Block(BLOCK_OVERVIEW);
    overviewMapBlock.write(block);
    block.write(stream);
    for (    DetailMapBlock detail : detailBlocks) {
      block=new Block(BLOCK_DETAIL);
      detail.write(block);
      block.write(stream);
    }
    if (tdbVersion >= TDB_V407) {
      tblock.setSum(stream.getChecksum().getValue());
      block=new Block(BLOCK_T);
      tblock.write(block);
      block.write(stream);
    }
  }
  finally {
    stream.close();
  }
}
 

Example 45

From project PSXperia, under directory /src/com/yifanlu/PSXperiaTool/.

Source file: ZpakCreate.java

  19 
vote

public static long getCRC32(File file) throws IOException {
  CheckedInputStream cis=null;
  long fileSize=0;
  cis=new CheckedInputStream(new FileInputStream(file),new CRC32());
  fileSize=file.length();
  byte[] buf=new byte[128];
  while (cis.read(buf) != -1)   ;
  long checksum=cis.getChecksum().getValue();
  cis.close();
  Logger.verbose("CRC32 of %s is %d",file.getPath(),checksum);
  return checksum;
}
 

Example 46

From project WaarpCommon, under directory /src/main/java/org/waarp/common/file/filesystembased/.

Source file: FilesystemBasedDirImpl.java

  19 
vote

public long getCRC(String path) throws CommandAbstractException {
  File file=getTrueFile(path);
  try {
    CheckedInputStream cis=null;
    try {
      cis=new CheckedInputStream(new FileInputStream(file),new CRC32());
    }
 catch (    FileNotFoundException e) {
      throw new Reply550Exception("File not found: " + path);
    }
    byte[] buf=new byte[session.getBlockSize()];
    while (cis.read(buf) >= 0) {
    }
    long result=cis.getChecksum().getValue();
    try {
      cis.close();
    }
 catch (    IOException e) {
    }
    return result;
  }
 catch (  IOException e) {
    throw new Reply550Exception("Error while reading file: " + path);
  }
}