View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.binary;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Random;
23  
24  /**
25   * This random data was encoded by OpenSSL. Java had nothing to do with it. This data helps us test interop between
26   * Commons-Codec and OpenSSL. Notice that OpenSSL creates 64 character lines instead of the 76 of Commons-Codec.
27   *
28   * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
29   * @version $Id $
30   * @since 1.4
31   */
32  public class Base32TestData {
33  
34      static final String STRING_FIXTURE = "Hello World";
35  
36      static final String BASE32_FIXTURE = "JBSWY3DPEBLW64TMMQ======\r\n";
37  //  static final String BASE32HEX_FIXTURE = "91IMOR3F41BMUSJCCG======";
38  
39      // Some utility code to help test chunked reads of the InputStream.
40  
41      private final static int SIZE_KEY = 0;
42      private final static int LAST_READ_KEY = 1;
43  
44      static byte[] streamToBytes(final InputStream in) throws IOException {
45          // new byte[7] is obviously quite slow, but helps exercise the code.
46          return streamToBytes(in, new byte[7]);
47      }
48  
49      static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException {
50          try {
51              int[] status = fill(buf, 0, in);
52              int size = status[SIZE_KEY];
53              int lastRead = status[LAST_READ_KEY];
54              while (lastRead != -1) {
55                  buf = resizeArray(buf);
56                  status = fill(buf, size, in);
57                  size = status[SIZE_KEY];
58                  lastRead = status[LAST_READ_KEY];
59              }
60              if (buf.length != size) {
61                  final byte[] smallerBuf = new byte[size];
62                  System.arraycopy(buf, 0, smallerBuf, 0, size);
63                  buf = smallerBuf;
64              }
65          }
66          finally {
67              in.close();
68          }
69          return buf;
70      }
71  
72      private static int[] fill(final byte[] buf, final int offset, final InputStream in)
73              throws IOException {
74          int read = in.read(buf, offset, buf.length - offset);
75          int lastRead = read;
76          if (read == -1) {
77              read = 0;
78          }
79          while (lastRead != -1 && read + offset < buf.length) {
80              lastRead = in.read(buf, offset + read, buf.length - read - offset);
81              if (lastRead != -1) {
82                  read += lastRead;
83              }
84          }
85          return new int[]{offset + read, lastRead};
86      }
87  
88      private static byte[] resizeArray(final byte[] bytes) {
89          final byte[] biggerBytes = new byte[bytes.length * 2];
90          System.arraycopy(bytes, 0, biggerBytes, 0, bytes.length);
91          return biggerBytes;
92      }
93  
94  
95      /**
96       * Returns an encoded and decoded copy of the same random data.
97       *
98       * @param codec the codec to use
99       * @param size amount of random data to generate and encode
100      * @return two byte[] arrays:  [0] = decoded, [1] = encoded
101      */
102     static byte[][] randomData(final BaseNCodec codec, final int size) {
103         final Random r = new Random();
104         final byte[] decoded = new byte[size];
105         r.nextBytes(decoded);
106         final byte[] encoded = codec.encode(decoded);
107         return new byte[][] {decoded, encoded};
108     }
109 
110     /**
111      * Tests the supplied byte[] array to see if it contains the specified byte c.
112      *
113      * @param bytes byte[] array to test
114      * @param c byte to look for
115      * @return true if bytes contains c, false otherwise
116      */
117     static boolean bytesContain(final byte[] bytes, final byte c) {
118         for (final byte b : bytes) {
119             if (b == c) { return true; }
120         }
121         return false;
122     }
123 
124 }