001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: *
015: * Alternatively, the contents of this file may be used under the terms of
016: * either of the GNU General Public License Version 2 or later (the "GPL"),
017: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
018: * in which case the provisions of the GPL or the LGPL are applicable instead
019: * of those above. If you wish to allow use of your version of this file only
020: * under the terms of either the GPL or the LGPL, and not to allow others to
021: * use your version of this file under the terms of the CPL, indicate your
022: * decision by deleting the provisions above and replace them with the notice
023: * and other provisions required by the GPL or the LGPL. If you do not delete
024: * the provisions above, a recipient may use your version of this file under
025: * the terms of any one of the CPL, the GPL or the LGPL.
026: ***** END LICENSE BLOCK *****/package org.jruby.util;
027:
028: import java.lang.reflect.Field;
029:
030: import java.util.zip.Adler32;
031: import java.util.zip.Checksum;
032:
033: /**
034: * This class is a wrapper around Adler32 which provides the capability to
035: * update the running total. This functionality is provided by quite risky
036: * reflection and should be fixed in a better way later on.
037: */
038: public class Adler32Ext implements Checksum {
039: private int adler;
040: private final Adler32 intern;
041:
042: private static final Field intern_adler;
043:
044: static {
045: try {
046: intern_adler = Adler32.class.getDeclaredField("adler");
047: intern_adler.setAccessible(true);
048: } catch (final NoSuchFieldException nsfe) {
049: throw new RuntimeException(
050: "This class have stopped working, it should be updated and FIXED now.");
051: }
052: }
053:
054: /**
055: * Creates the basic object with default initial adler.
056: */
057: public Adler32Ext() {
058: this (1);
059: }
060:
061: /**
062: * Creates the basic object with the adler provided.
063: *
064: * @param adler the number to use as starting point for the Adler-32 algorithm
065: */
066: public Adler32Ext(final int adler) {
067: super ();
068: this .adler = adler;
069: this .intern = new Adler32();
070: setAdlerRef(this .adler);
071: }
072:
073: /**
074: * Sets the adler running total to the specified value.
075: *
076: * @param adler the number to use as current value for the Adler-32 algorithm
077: */
078: public void setAdler(final int adler) {
079: this .adler = adler;
080: setAdlerRef(this .adler);
081: }
082:
083: /**
084: * @see java.util.zip.Checksum#update
085: */
086: public void update(final int b) {
087: this .intern.update(b);
088: }
089:
090: /**
091: * @see java.util.zip.Checksum#update
092: */
093: public void update(final byte[] b, final int off, final int len) {
094: this .intern.update(b, off, len);
095: }
096:
097: /**
098: * @see java.util.zip.Checksum#update
099: */
100: public void update(final byte[] b) {
101: this .intern.update(b);
102: }
103:
104: /**
105: * @see java.util.zip.Checksum#reset
106: */
107: public void reset() {
108: this .intern.reset();
109: this .adler = 1;
110: }
111:
112: /**
113: * @see java.util.zip.Checksum#getValue
114: */
115: public long getValue() {
116: return this .intern.getValue();
117: }
118:
119: /**
120: * Helper method to set the reference through reflection.
121: *
122: * @param val the value to set.
123: */
124: private void setAdlerRef(final int val) {
125: try {
126: intern_adler.setInt(intern, val);
127: } catch (final IllegalAccessException e) {
128: throw new IllegalStateException(e.toString());
129: }
130: }
131: }
|