1 /* BasePRNG.java --
2    Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
3 
4 This file is a part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version.  */
37 
38 
39 package gnu.java.security.prng;
40 
41 import java.util.Map;
42 
43 /**
44  * An abstract class to facilitate implementing PRNG algorithms.
45  */
46 public abstract class BasePRNG
47     implements IRandom
48 {
49   /** The canonical name prefix of the PRNG algorithm. */
50   protected String name;
51 
52   /** Indicate if this instance has already been initialised or not. */
53   protected boolean initialised;
54 
55   /** A temporary buffer to serve random bytes. */
56   protected byte[] buffer;
57 
58   /** The index into buffer of where the next byte will come from. */
59   protected int ndx;
60 
61   /**
62    * Trivial constructor for use by concrete subclasses.
63    *
64    * @param name the canonical name of this instance.
65    */
BasePRNG(String name)66   protected BasePRNG(String name)
67   {
68     super();
69 
70     this.name = name;
71     initialised = false;
72     buffer = new byte[0];
73   }
74 
name()75   public String name()
76   {
77     return name;
78   }
79 
init(Map attributes)80   public void init(Map attributes)
81   {
82     this.setup(attributes);
83 
84     ndx = 0;
85     initialised = true;
86   }
87 
nextByte()88   public byte nextByte() throws IllegalStateException, LimitReachedException
89   {
90     if (! initialised)
91       throw new IllegalStateException();
92 
93     return nextByteInternal();
94   }
95 
nextBytes(byte[] out)96   public void nextBytes(byte[] out) throws IllegalStateException,
97       LimitReachedException
98   {
99     nextBytes(out, 0, out.length);
100   }
101 
nextBytes(byte[] out, int offset, int length)102   public void nextBytes(byte[] out, int offset, int length)
103       throws IllegalStateException, LimitReachedException
104   {
105     if (! initialised)
106       throw new IllegalStateException("not initialized");
107 
108     if (length == 0)
109       return;
110 
111     if (offset < 0 || length < 0 || offset + length > out.length)
112       throw new ArrayIndexOutOfBoundsException("offset=" + offset + " length="
113                                                + length + " limit="
114                                                + out.length);
115     if (ndx >= buffer.length)
116       {
117         fillBlock();
118         ndx = 0;
119       }
120     int count = 0;
121     while (count < length)
122       {
123         int amount = Math.min(buffer.length - ndx, length - count);
124         System.arraycopy(buffer, ndx, out, offset + count, amount);
125         count += amount;
126         ndx += amount;
127         if (ndx >= buffer.length)
128           {
129             fillBlock();
130             ndx = 0;
131           }
132       }
133   }
134 
addRandomByte(byte b)135   public void addRandomByte(byte b)
136   {
137     throw new UnsupportedOperationException("random state is non-modifiable");
138   }
139 
addRandomBytes(byte[] buffer)140   public void addRandomBytes(byte[] buffer)
141   {
142     addRandomBytes(buffer, 0, buffer.length);
143   }
144 
addRandomBytes(byte[] buffer, int offset, int length)145   public void addRandomBytes(byte[] buffer, int offset, int length)
146   {
147     throw new UnsupportedOperationException("random state is non-modifiable");
148   }
149 
isInitialised()150   public boolean isInitialised()
151   {
152     return initialised;
153   }
154 
nextByteInternal()155   private byte nextByteInternal() throws LimitReachedException
156   {
157     if (ndx >= buffer.length)
158       {
159         this.fillBlock();
160         ndx = 0;
161       }
162 
163     return buffer[ndx++];
164   }
165 
clone()166   public Object clone() throws CloneNotSupportedException
167   {
168     BasePRNG result = (BasePRNG) super.clone();
169     if (this.buffer != null)
170       result.buffer = (byte[]) this.buffer.clone();
171 
172     return result;
173   }
174 
setup(Map attributes)175   public abstract void setup(Map attributes);
176 
fillBlock()177   public abstract void fillBlock() throws LimitReachedException;
178 }
179