KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > Adler32Ext


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  *
15  * Alternatively, the contents of this file may be used under the terms of
16  * either of the GNU General Public License Version 2 or later (the "GPL"),
17  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
18  * in which case the provisions of the GPL or the LGPL are applicable instead
19  * of those above. If you wish to allow use of your version of this file only
20  * under the terms of either the GPL or the LGPL, and not to allow others to
21  * use your version of this file under the terms of the CPL, indicate your
22  * decision by deleting the provisions above and replace them with the notice
23  * and other provisions required by the GPL or the LGPL. If you do not delete
24  * the provisions above, a recipient may use your version of this file under
25  * the terms of any one of the CPL, the GPL or the LGPL.
26  ***** END LICENSE BLOCK *****/

27 package org.jruby.util;
28
29 import java.lang.reflect.Field JavaDoc;
30
31 import java.util.zip.Adler32 JavaDoc;
32 import java.util.zip.Checksum JavaDoc;
33
34 /**
35  * This class is a wrapper around Adler32 which provides the capability to
36  * update the running total. This functionality is provided by quite risky
37  * reflection and should be fixed in a better way later on.
38  */

39 public class Adler32Ext implements Checksum JavaDoc {
40     private int adler;
41     private final Adler32 JavaDoc intern;
42
43     private static final Field JavaDoc intern_adler;
44
45     static {
46         try {
47             intern_adler = Adler32 JavaDoc.class.getDeclaredField("adler");
48             intern_adler.setAccessible(true);
49         } catch(final NoSuchFieldException JavaDoc nsfe) {
50             throw new RuntimeException JavaDoc("This class have stopped working, it should be updated and FIXED now.");
51         }
52     }
53
54     /**
55      * Creates the basic object with default initial adler.
56      */

57     public Adler32Ext() {
58         this(1);
59     }
60
61     /**
62      * Creates the basic object with the adler provided.
63      *
64      * @param adler the number to use as starting point for the Adler-32 algorithm
65      */

66     public Adler32Ext(final int adler) {
67         super();
68         this.adler=adler;
69         this.intern = new Adler32 JavaDoc();
70         setAdlerRef(this.adler);
71     }
72
73     /**
74      * Sets the adler running total to the specified value.
75      *
76      * @param adler the number to use as current value for the Adler-32 algorithm
77      */

78     public void setAdler(final int adler) {
79         this.adler = adler;
80         setAdlerRef(this.adler);
81     }
82
83     /**
84      * @see java.util.zip.Checksum#update
85      */

86     public void update(final int b) {
87         this.intern.update(b);
88     }
89
90     /**
91      * @see java.util.zip.Checksum#update
92      */

93     public void update(final byte[] b, final int off, final int len) {
94         this.intern.update(b,off,len);
95     }
96
97     /**
98      * @see java.util.zip.Checksum#update
99      */

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 JavaDoc e) {
128             throw new IllegalStateException JavaDoc(e.toString());
129         }
130     }
131 }
132
Popular Tags