diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/util')
36 files changed, 696 insertions, 38 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java index 3f7677ca..3c0646ab 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java @@ -1,6 +1,7 @@ package org.bouncycastle.util; import java.math.BigInteger; +import java.util.Iterator; /** * General array utilities. @@ -323,6 +324,25 @@ public final class Arrays return hc; } + + public static int hashCode(byte[] data, int off, int len) + { + if (data == null) + { + return 0; + } + + int i = len; + int hc = i + 1; + + while (--i >= 0) + { + hc *= 257; + hc ^= data[off + i]; + } + + return hc; + } public static int hashCode(char[] data) { @@ -374,6 +394,25 @@ public final class Arrays return hc; } + public static int hashCode(int[] data, int off, int len) + { + if (data == null) + { + return 0; + } + + int i = len; + int hc = i + 1; + + while (--i >= 0) + { + hc *= 257; + hc ^= data[off + i]; + } + + return hc; + } + public static int hashCode(short[][][] shorts) { int hc = 0; @@ -752,6 +791,20 @@ public final class Arrays return result; } + public static short[] append(short[] a, short b) + { + if (a == null) + { + return new short[]{ b }; + } + + int length = a.length; + short[] result = new short[length + 1]; + System.arraycopy(a, 0, result, 0, length); + result[length] = b; + return result; + } + public static int[] append(int[] a, int b) { if (a == null) @@ -840,6 +893,23 @@ public final class Arrays } } + public static int[] concatenate(int[] a, int[] b) + { + if (a == null) + { + return clone(b); + } + if (b == null) + { + return clone(a); + } + + int[] c = new int[a.length + b.length]; + System.arraycopy(a, 0, c, 0, a.length); + System.arraycopy(b, 0, c, a.length, b.length); + return c; + } + public static byte[] prepend(byte[] a, byte b) { if (a == null) @@ -853,4 +923,89 @@ public final class Arrays result[0] = b; return result; } + + public static short[] prepend(short[] a, short b) + { + if (a == null) + { + return new short[]{ b }; + } + + int length = a.length; + short[] result = new short[length + 1]; + System.arraycopy(a, 0, result, 1, length); + result[0] = b; + return result; + } + + public static int[] prepend(int[] a, int b) + { + if (a == null) + { + return new int[]{ b }; + } + + int length = a.length; + int[] result = new int[length + 1]; + System.arraycopy(a, 0, result, 1, length); + result[0] = b; + return result; + } + + public static byte[] reverse(byte[] a) + { + if (a == null) + { + return null; + } + + int p1 = 0, p2 = a.length; + byte[] result = new byte[p2]; + + while (--p2 >= 0) + { + result[p2] = a[p1++]; + } + + return result; + } + + /** + * Iterator backed by a specific array. + */ + public static class Iterator<T> + implements java.util.Iterator<T> + { + private final T[] dataArray; + + private int position = 0; + + /** + * Base constructor. + * <p> + * Note: the array is not cloned, changes to it will affect the values returned by next(). + * </p> + * + * @param dataArray array backing the iterator. + */ + public Iterator(T[] dataArray) + { + this.dataArray = dataArray; + } + + public boolean hasNext() + { + return position < dataArray.length; + } + + public T next() + { + return dataArray[position++]; + } + + public void remove() + { + throw new UnsupportedOperationException("Cannot remove element from an Array."); + } + } } diff --git a/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java b/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java index 91aba146..1cc9641a 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java +++ b/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java @@ -8,10 +8,10 @@ import java.util.List; /** * A simple collection backed store. */ -public class CollectionStore - implements Store +public class CollectionStore<T> + implements Store<T>, Iterable<T> { - private Collection _local; + private Collection<T> _local; /** * Basic constructor. @@ -19,9 +19,9 @@ public class CollectionStore * @param collection - initial contents for the store, this is copied. */ public CollectionStore( - Collection collection) + Collection<T> collection) { - _local = new ArrayList(collection); + _local = new ArrayList<T>(collection); } /** @@ -30,20 +30,20 @@ public class CollectionStore * @param selector the selector to match against. * @return a possibly empty collection of matching objects. */ - public Collection getMatches(Selector selector) + public Collection<T> getMatches(Selector<T> selector) { if (selector == null) { - return new ArrayList(_local); + return new ArrayList<T>(_local); } else { - List col = new ArrayList(); - Iterator iter = _local.iterator(); + List<T> col = new ArrayList<T>(); + Iterator<T> iter = _local.iterator(); while (iter.hasNext()) { - Object obj = iter.next(); + T obj = iter.next(); if (selector.match(obj)) { @@ -54,4 +54,9 @@ public class CollectionStore return col; } } + + public Iterator<T> iterator() + { + return getMatches(null).iterator(); + } } diff --git a/bcprov/src/main/java/org/bouncycastle/util/Encodable.java b/bcprov/src/main/java/org/bouncycastle/util/Encodable.java new file mode 100644 index 00000000..67258fb4 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Encodable.java @@ -0,0 +1,18 @@ +package org.bouncycastle.util; + +import java.io.IOException; + +/** + * Interface implemented by objects that can be converted into byte arrays. + */ +public interface Encodable +{ + /** + * Return a byte array representing the implementing object. + * + * @return a byte array representing the encoding. + * @throws java.io.IOException if an issue arises generation the encoding. + */ + byte[] getEncoded() + throws IOException; +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Integers.java b/bcprov/src/main/java/org/bouncycastle/util/Integers.java index 599a9e0c..f52baf52 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Integers.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Integers.java @@ -2,6 +2,16 @@ package org.bouncycastle.util; public class Integers { + public static int rotateLeft(int i, int distance) + { + return Integer.rotateLeft(i, distance); + } + + public static int rotateRight(int i, int distance) + { + return Integer.rotateRight(i, distance); + } + public static Integer valueOf(int value) { return Integer.valueOf(value); diff --git a/bcprov/src/main/java/org/bouncycastle/util/Iterable.java b/bcprov/src/main/java/org/bouncycastle/util/Iterable.java new file mode 100644 index 00000000..a0fa9dcc --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Iterable.java @@ -0,0 +1,17 @@ +package org.bouncycastle.util; + +import java.util.Iterator; + +/** + * Utility class to allow use of Iterable feature in JDK 1.5+ + */ +public interface Iterable<T> + extends java.lang.Iterable<T> +{ + /** + * Returns an iterator over a set of elements of type T. + * + * @return an Iterator. + */ + Iterator<T> iterator(); +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Memoable.java b/bcprov/src/main/java/org/bouncycastle/util/Memoable.java index 0be91711..ee0bedac 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Memoable.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Memoable.java @@ -4,7 +4,7 @@ public interface Memoable { /** * Produce a copy of this object with its configuration and in its current state. - * <p/> + * <p> * The returned object may be used simply to store the state, or may be used as a similar object * starting from the copied state. */ @@ -12,7 +12,7 @@ public interface Memoable /** * Restore a copied object state into this object. - * <p/> + * <p> * Implementations of this method <em>should</em> try to avoid or minimise memory allocation to perform the reset. * * @param other an object originally {@link #copy() copied} from an object of the same type as this instance. diff --git a/bcprov/src/main/java/org/bouncycastle/util/Pack.java b/bcprov/src/main/java/org/bouncycastle/util/Pack.java new file mode 100644 index 00000000..2f96af84 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Pack.java @@ -0,0 +1,201 @@ +package org.bouncycastle.util; + +public abstract class Pack +{ + public static int bigEndianToInt(byte[] bs, int off) + { + int n = bs[ off] << 24; + n |= (bs[++off] & 0xff) << 16; + n |= (bs[++off] & 0xff) << 8; + n |= (bs[++off] & 0xff); + return n; + } + + public static void bigEndianToInt(byte[] bs, int off, int[] ns) + { + for (int i = 0; i < ns.length; ++i) + { + ns[i] = bigEndianToInt(bs, off); + off += 4; + } + } + + public static byte[] intToBigEndian(int n) + { + byte[] bs = new byte[4]; + intToBigEndian(n, bs, 0); + return bs; + } + + public static void intToBigEndian(int n, byte[] bs, int off) + { + bs[ off] = (byte)(n >>> 24); + bs[++off] = (byte)(n >>> 16); + bs[++off] = (byte)(n >>> 8); + bs[++off] = (byte)(n ); + } + + public static byte[] intToBigEndian(int[] ns) + { + byte[] bs = new byte[4 * ns.length]; + intToBigEndian(ns, bs, 0); + return bs; + } + + public static void intToBigEndian(int[] ns, byte[] bs, int off) + { + for (int i = 0; i < ns.length; ++i) + { + intToBigEndian(ns[i], bs, off); + off += 4; + } + } + + public static long bigEndianToLong(byte[] bs, int off) + { + int hi = bigEndianToInt(bs, off); + int lo = bigEndianToInt(bs, off + 4); + return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); + } + + public static void bigEndianToLong(byte[] bs, int off, long[] ns) + { + for (int i = 0; i < ns.length; ++i) + { + ns[i] = bigEndianToLong(bs, off); + off += 8; + } + } + + public static byte[] longToBigEndian(long n) + { + byte[] bs = new byte[8]; + longToBigEndian(n, bs, 0); + return bs; + } + + public static void longToBigEndian(long n, byte[] bs, int off) + { + intToBigEndian((int)(n >>> 32), bs, off); + intToBigEndian((int)(n & 0xffffffffL), bs, off + 4); + } + + public static byte[] longToBigEndian(long[] ns) + { + byte[] bs = new byte[8 * ns.length]; + longToBigEndian(ns, bs, 0); + return bs; + } + + public static void longToBigEndian(long[] ns, byte[] bs, int off) + { + for (int i = 0; i < ns.length; ++i) + { + longToBigEndian(ns[i], bs, off); + off += 8; + } + } + + public static int littleEndianToInt(byte[] bs, int off) + { + int n = bs[ off] & 0xff; + n |= (bs[++off] & 0xff) << 8; + n |= (bs[++off] & 0xff) << 16; + n |= bs[++off] << 24; + return n; + } + + public static void littleEndianToInt(byte[] bs, int off, int[] ns) + { + for (int i = 0; i < ns.length; ++i) + { + ns[i] = littleEndianToInt(bs, off); + off += 4; + } + } + + public static void littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count) + { + for (int i = 0; i < count; ++i) + { + ns[nOff + i] = littleEndianToInt(bs, bOff); + bOff += 4; + } + } + + public static byte[] intToLittleEndian(int n) + { + byte[] bs = new byte[4]; + intToLittleEndian(n, bs, 0); + return bs; + } + + public static void intToLittleEndian(int n, byte[] bs, int off) + { + bs[ off] = (byte)(n ); + bs[++off] = (byte)(n >>> 8); + bs[++off] = (byte)(n >>> 16); + bs[++off] = (byte)(n >>> 24); + } + + public static byte[] intToLittleEndian(int[] ns) + { + byte[] bs = new byte[4 * ns.length]; + intToLittleEndian(ns, bs, 0); + return bs; + } + + public static void intToLittleEndian(int[] ns, byte[] bs, int off) + { + for (int i = 0; i < ns.length; ++i) + { + intToLittleEndian(ns[i], bs, off); + off += 4; + } + } + + public static long littleEndianToLong(byte[] bs, int off) + { + int lo = littleEndianToInt(bs, off); + int hi = littleEndianToInt(bs, off + 4); + return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); + } + + public static void littleEndianToLong(byte[] bs, int off, long[] ns) + { + for (int i = 0; i < ns.length; ++i) + { + ns[i] = littleEndianToLong(bs, off); + off += 8; + } + } + + public static byte[] longToLittleEndian(long n) + { + byte[] bs = new byte[8]; + longToLittleEndian(n, bs, 0); + return bs; + } + + public static void longToLittleEndian(long n, byte[] bs, int off) + { + intToLittleEndian((int)(n & 0xffffffffL), bs, off); + intToLittleEndian((int)(n >>> 32), bs, off + 4); + } + + public static byte[] longToLittleEndian(long[] ns) + { + byte[] bs = new byte[8 * ns.length]; + longToLittleEndian(ns, bs, 0); + return bs; + } + + public static void longToLittleEndian(long[] ns, byte[] bs, int off) + { + for (int i = 0; i < ns.length; ++i) + { + longToLittleEndian(ns[i], bs, off); + off += 8; + } + } +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Selector.java b/bcprov/src/main/java/org/bouncycastle/util/Selector.java index 7ad86bf4..a3a5ec86 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Selector.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Selector.java @@ -1,9 +1,9 @@ package org.bouncycastle.util; -public interface Selector +public interface Selector<T> extends Cloneable { - boolean match(Object obj); + boolean match(T obj); Object clone(); } diff --git a/bcprov/src/main/java/org/bouncycastle/util/Store.java b/bcprov/src/main/java/org/bouncycastle/util/Store.java index b994c926..fd28b838 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Store.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Store.java @@ -2,8 +2,8 @@ package org.bouncycastle.util; import java.util.Collection; -public interface Store +public interface Store<T> { - Collection getMatches(Selector selector) + Collection<T> getMatches(Selector<T> selector) throws StoreException; } diff --git a/bcprov/src/main/java/org/bouncycastle/util/StringList.java b/bcprov/src/main/java/org/bouncycastle/util/StringList.java new file mode 100644 index 00000000..e7334427 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/StringList.java @@ -0,0 +1,22 @@ +package org.bouncycastle.util; + +public interface StringList + extends Iterable<String> +{ + boolean add(String s); + + String get(int index); + + int size(); + + String[] toStringArray(); + + /** + * Return a section of the contents of the list. If the list is too short the array is filled with nulls. + * + * @param from the initial index of the range to be copied, inclusive + * @param to the final index of the range to be copied, exclusive. + * @return an array of length to - from + */ + String[] toStringArray(int from, int to); +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Strings.java b/bcprov/src/main/java/org/bouncycastle/util/Strings.java index 7f674046..44ff3aec 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Strings.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Strings.java @@ -3,6 +3,7 @@ package org.bouncycastle.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Vector; public final class Strings @@ -241,6 +242,17 @@ public final class Strings return bytes; } + public static int toByteArray(String s, byte[] buf, int off) + { + int count = s.length(); + for (int i = 0; i < count; ++i) + { + char c = s.charAt(i); + buf[off + i] = (byte)c; + } + return count; + } + /** * Convert an array of 8 bit characters into a string. * @@ -300,4 +312,53 @@ public final class Strings } return res; } + + public static StringList newList() + { + return new StringListImpl(); + } + + private static class StringListImpl + extends ArrayList<String> + implements StringList + { + public boolean add(String s) + { + return super.add(s); + } + + public String set(int index, String element) + { + return super.set(index, element); + } + + public void add(int index, String element) + { + super.add(index, element); + } + + public String[] toStringArray() + { + String[] strs = new String[this.size()]; + + for (int i = 0; i != strs.length; i++) + { + strs[i] = this.get(i); + } + + return strs; + } + + public String[] toStringArray(int from, int to) + { + String[] strs = new String[to - from]; + + for (int i = from; i != this.size() && i != to; i++) + { + strs[i - from] = this.get(i); + } + + return strs; + } + } } diff --git a/bcprov/src/main/java/org/bouncycastle/util/Times.java b/bcprov/src/main/java/org/bouncycastle/util/Times.java new file mode 100644 index 00000000..617d00bd --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Times.java @@ -0,0 +1,9 @@ +package org.bouncycastle.util; + +public final class Times +{ + public static long nanoTime() + { + return System.nanoTime(); + } +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64.java index 83806295..c04a8cc1 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64.java @@ -6,6 +6,9 @@ import java.io.OutputStream; import org.bouncycastle.util.Strings; +/** + * Utility class for converting Base64 data to bytes and back again. + */ public class Base64 { private static final Encoder encoder = new Base64Encoder(); diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java index 1ef8f51b..abad02c5 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java @@ -3,24 +3,27 @@ package org.bouncycastle.util.encoders; import java.io.IOException; import java.io.OutputStream; +/** + * A streaming Base64 encoder. + */ public class Base64Encoder implements Encoder { protected final byte[] encodingTable = - { - (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', - (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', - (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', - (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', - (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', - (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', - (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', - (byte)'v', - (byte)'w', (byte)'x', (byte)'y', (byte)'z', - (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', - (byte)'7', (byte)'8', (byte)'9', - (byte)'+', (byte)'/' - }; + { + (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', + (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', + (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', + (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', + (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', + (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', + (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', + (byte)'v', + (byte)'w', (byte)'x', (byte)'y', (byte)'z', + (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', + (byte)'7', (byte)'8', (byte)'9', + (byte)'+', (byte)'/' + }; protected byte padding = (byte)'='; diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java index 672430ac..eea85b9f 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java @@ -2,7 +2,7 @@ package org.bouncycastle.util.encoders; /** - * a buffering class to allow translation from one format to another to + * A buffering class to allow translation from one format to another to * be done in discrete chunks. */ public class BufferedDecoder diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java index 107eee89..60a098de 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java @@ -2,7 +2,7 @@ package org.bouncycastle.util.encoders; /** - * a buffering class to allow translation from one format to another to + * A buffering class to allow translation from one format to another to * be done in discrete chunks. */ public class BufferedEncoder diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/DecoderException.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/DecoderException.java index d9914a22..1e6782a5 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/DecoderException.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/DecoderException.java @@ -1,5 +1,8 @@ package org.bouncycastle.util.encoders; +/** + * Exception thrown if an attempt is made to decode invalid data, or some other failure occurs. + */ public class DecoderException extends IllegalStateException { diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/EncoderException.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/EncoderException.java index 2d09a63a..a1eb411e 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/EncoderException.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/EncoderException.java @@ -1,5 +1,8 @@ package org.bouncycastle.util.encoders; +/** + * Exception thrown if an attempt is made to encode invalid data, or some other failure occurs. + */ public class EncoderException extends IllegalStateException { diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/Hex.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/Hex.java index d49f1ef4..63e9c713 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/Hex.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/Hex.java @@ -6,6 +6,9 @@ import java.io.OutputStream; import org.bouncycastle.util.Strings; +/** + * Utility class for converting hex data to bytes and back again. + */ public class Hex { private static final Encoder encoder = new HexEncoder(); diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java index 3bb594b0..52f8fa6d 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java @@ -3,15 +3,18 @@ package org.bouncycastle.util.encoders; import java.io.IOException; import java.io.OutputStream; +/** + * A streaming Hex encoder. + */ public class HexEncoder implements Encoder { protected final byte[] encodingTable = - { - (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', - (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' - }; - + { + (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', + (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' + }; + /* * set up the decoding table. */ diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/Translator.java b/bcprov/src/main/java/org/bouncycastle/util/encoders/Translator.java index a3a0cb8e..96381bcf 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/encoders/Translator.java +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/Translator.java @@ -1,7 +1,7 @@ package org.bouncycastle.util.encoders; /** - * general interface for an translator. + * General interface for an translator. */ public interface Translator { diff --git a/bcprov/src/main/java/org/bouncycastle/util/encoders/package.html b/bcprov/src/main/java/org/bouncycastle/util/encoders/package.html new file mode 100644 index 00000000..3be222b2 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/encoders/package.html @@ -0,0 +1,5 @@ +<html> +<body bgcolor="#ffffff"> +Classes for producing and reading Base64 and Hex strings. +</body> +</html> diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/StreamOverflowException.java b/bcprov/src/main/java/org/bouncycastle/util/io/StreamOverflowException.java index 01af8da9..ed5518d7 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/StreamOverflowException.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/StreamOverflowException.java @@ -2,6 +2,9 @@ package org.bouncycastle.util.io; import java.io.IOException; +/** + * Exception thrown when too much data is written to an InputStream + */ public class StreamOverflowException extends IOException { diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/Streams.java b/bcprov/src/main/java/org/bouncycastle/util/io/Streams.java index 41560b5d..0dea2369 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/Streams.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/Streams.java @@ -5,10 +5,19 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +/** + * Utility methods to assist with stream processing. + */ public final class Streams { private static int BUFFER_SIZE = 512; + /** + * Read stream till EOF is encountered. + * + * @param inStr stream to be emptied. + * @throws IOException in case of underlying IOException. + */ public static void drain(InputStream inStr) throws IOException { @@ -18,6 +27,13 @@ public final class Streams } } + /** + * Read stream fully, returning contents in a byte array. + * + * @param inStr stream to be read. + * @return a byte array representing the contents of inStr. + * @throws IOException in case of underlying IOException. + */ public static byte[] readAll(InputStream inStr) throws IOException { @@ -26,6 +42,15 @@ public final class Streams return buf.toByteArray(); } + /** + * Read from inStr up to a maximum number of bytes, throwing an exception if more the maximum amount + * of requested data is available. + * + * @param inStr stream to be read. + * @param limit maximum number of bytes that can be read. + * @return a byte array representing the contents of inStr. + * @throws IOException in case of underlying IOException, or if limit is reached on inStr still has data in it. + */ public static byte[] readAllLimited(InputStream inStr, int limit) throws IOException { @@ -34,12 +59,30 @@ public final class Streams return buf.toByteArray(); } + /** + * Fully read in buf's length in data, or up to EOF, whichever occurs first, + * + * @param inStr the stream to be read. + * @param buf the buffer to be read into. + * @return the number of bytes read into the buffer. + * @throws IOException in case of underlying IOException. + */ public static int readFully(InputStream inStr, byte[] buf) throws IOException { return readFully(inStr, buf, 0, buf.length); } + /** + * Fully read in len's bytes of data into buf, or up to EOF, whichever occurs first, + * + * @param inStr the stream to be read. + * @param buf the buffer to be read into. + * @param off offset into buf to start putting bytes into. + * @param len the number of bytes to be read. + * @return the number of bytes read into the buffer. + * @throws IOException in case of underlying IOException. + */ public static int readFully(InputStream inStr, byte[] buf, int off, int len) throws IOException { @@ -56,6 +99,13 @@ public final class Streams return totalRead; } + /** + * Write the full contents of inStr to the destination stream outStr. + * + * @param inStr source input stream. + * @param outStr destination output stream. + * @throws IOException in case of underlying IOException. + */ public static void pipeAll(InputStream inStr, OutputStream outStr) throws IOException { @@ -67,6 +117,14 @@ public final class Streams } } + /** + * Write up to limit bytes of data from inStr to the destination stream outStr. + * + * @param inStr source input stream. + * @param limit the maximum number of bytes allowed to be read. + * @param outStr destination output stream. + * @throws IOException in case of underlying IOException, or if limit is reached on inStr still has data in it. + */ public static long pipeAllLimited(InputStream inStr, long limit, OutputStream outStr) throws IOException { diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/TeeInputStream.java b/bcprov/src/main/java/org/bouncycastle/util/io/TeeInputStream.java index 91542469..96da1694 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/TeeInputStream.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/TeeInputStream.java @@ -4,12 +4,21 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +/** + * An input stream which copies anything read through it to another stream. + */ public class TeeInputStream extends InputStream { private final InputStream input; private final OutputStream output; + /** + * Base constructor. + * + * @param input input stream to be wrapped. + * @param output output stream to copy any input read to. + */ public TeeInputStream(InputStream input, OutputStream output) { this.input = input; diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/TeeOutputStream.java b/bcprov/src/main/java/org/bouncycastle/util/io/TeeOutputStream.java index a4919cd8..05b2b563 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/TeeOutputStream.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/TeeOutputStream.java @@ -3,12 +3,22 @@ package org.bouncycastle.util.io; import java.io.IOException; import java.io.OutputStream; + +/** + * An output stream which copies anything written into it to another stream. + */ public class TeeOutputStream extends OutputStream { private OutputStream output1; private OutputStream output2; + /** + * Base constructor. + * + * @param output1 the output stream that is wrapped. + * @param output2 a secondary stream that anything written to output1 is also written to. + */ public TeeOutputStream(OutputStream output1, OutputStream output2) { this.output1 = output1; diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/package.html b/bcprov/src/main/java/org/bouncycastle/util/io/package.html new file mode 100644 index 00000000..e987ccaf --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/io/package.html @@ -0,0 +1,5 @@ +<html> +<body bgcolor="#ffffff"> +General purpose I/O helper classes and wrappers. +</body> +</html> diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemGenerationException.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemGenerationException.java index 69a773e7..63f61f2d 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemGenerationException.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemGenerationException.java @@ -2,6 +2,9 @@ package org.bouncycastle.util.io.pem; import java.io.IOException; +/** + * Exception thrown on failure to generate a PEM object. + */ public class PemGenerationException extends IOException { diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemHeader.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemHeader.java index b201c134..bbc6108e 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemHeader.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemHeader.java @@ -1,10 +1,19 @@ package org.bouncycastle.util.io.pem; +/** + * Class representing a PEM header (name, value) pair. + */ public class PemHeader { private String name; private String value; + /** + * Base constructor. + * + * @param name name of the header property. + * @param value value of the header property. + */ public PemHeader(String name, String value) { this.name = name; diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObject.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObject.java index 2199520b..606330d8 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObject.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObject.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +/** + * A generic PEM object - type, header properties, and byte content. + */ public class PemObject implements PemObjectGenerator { diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectGenerator.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectGenerator.java index 6fffdc58..96646392 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectGenerator.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectGenerator.java @@ -1,7 +1,16 @@ package org.bouncycastle.util.io.pem; +/** + * Base interface for generators of PEM objects. + */ public interface PemObjectGenerator { + /** + * Generate a PEM object. + * + * @return the generated object. + * @throws PemGenerationException on failure. + */ PemObject generate() throws PemGenerationException; } diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectParser.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectParser.java index b18b5505..933da6ab 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectParser.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemObjectParser.java @@ -2,8 +2,18 @@ package org.bouncycastle.util.io.pem; import java.io.IOException; +/** + * Base interface for parsers to convert PEM objects into specific objects. + */ public interface PemObjectParser { + /** + * Parse an object out of the PEM object passed in. + * + * @param obj the PEM object containing the details for the specific object. + * @return a specific object represented by the PEM object. + * @throws IOException on a parsing error. + */ Object parseObject(PemObject obj) throws IOException; } diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemReader.java b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemReader.java index 76647257..3045b4d0 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemReader.java +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/PemReader.java @@ -8,6 +8,9 @@ import java.util.List; import org.bouncycastle.util.encoders.Base64; +/** + * A generic PEM reader, based on the format outlined in RFC 1421 + */ public class PemReader extends BufferedReader { diff --git a/bcprov/src/main/java/org/bouncycastle/util/io/pem/package.html b/bcprov/src/main/java/org/bouncycastle/util/io/pem/package.html new file mode 100644 index 00000000..c5cc4a5d --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/io/pem/package.html @@ -0,0 +1,5 @@ +<html> +<body bgcolor="#ffffff"> +Classes for reading and writing raw PEM objects. +</body> +</html> diff --git a/bcprov/src/main/java/org/bouncycastle/util/package.html b/bcprov/src/main/java/org/bouncycastle/util/package.html new file mode 100644 index 00000000..c9247ea4 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/package.html @@ -0,0 +1,5 @@ +<html> +<body bgcolor="#ffffff"> +General purpose utility classes used throughout the APIs. +</body> +</html> diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/package.html b/bcprov/src/main/java/org/bouncycastle/util/test/package.html new file mode 100644 index 00000000..e723fd11 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/test/package.html @@ -0,0 +1,5 @@ +<html> +<body bgcolor="#ffffff"> +Light weight test API. If you can use Junit! +</body> +</html> |