1 27 package org.jruby.util; 28 29 import java.lang.reflect.Field ; 30 31 import java.util.zip.CRC32 ; 32 import java.util.zip.Checksum ; 33 34 39 public class CRC32Ext implements Checksum { 40 private int crc; 41 private final CRC32 intern; 42 43 private static final Field intern_crc; 44 45 static { 46 try { 47 intern_crc = CRC32 .class.getDeclaredField("crc"); 48 intern_crc.setAccessible(true); 49 } catch(final NoSuchFieldException nsfe) { 50 throw new RuntimeException ("This class have stopped working, it should be updated and FIXED now."); 51 } 52 } 53 54 57 public CRC32Ext() { 58 this(1); 59 } 60 61 66 public CRC32Ext(final int crc) { 67 super(); 68 this.crc=crc; 69 this.intern = new CRC32 (); 70 setCRCRef(this.crc); 71 } 72 73 78 public void setAdler(final int crc) { 79 this.crc = crc; 80 setCRCRef(this.crc); 81 } 82 83 86 public void update(final int b) { 87 this.intern.update(b); 88 } 89 90 93 public void update(final byte[] b, final int off, final int len) { 94 this.intern.update(b,off,len); 95 } 96 97 100 public void update(final byte[] b) { 101 this.intern.update(b); 102 } 103 104 107 public void reset() { 108 this.intern.reset(); 109 this.crc = 1; 110 } 111 112 115 public long getValue() { 116 return this.intern.getValue(); 117 } 118 119 124 private void setCRCRef(final int val) { 125 try { 126 intern_crc.setInt(intern,val); 127 } catch(final IllegalAccessException e) { 128 throw new IllegalStateException (e.toString()); 129 } 130 } 131 } 132 | Popular Tags |