예제 #1
0
  /**
   * Convenience method for reading a binary file and base64-encoding it.
   *
   * @param filename Filename for reading binary data
   * @return base64-encoded string or null if unsuccessful
   * @since 2.1
   */
  public static String encodeFromFile(String filename) {
    String encodedData = null;
    Base64.InputStream bis = null;
    try {
      // Set up some useful variables
      java.io.File file = new java.io.File(filename);
      byte[] buffer = new byte[(int) (file.length() * 1.4)];
      int length = 0;
      int numBytes = 0;

      // Open a stream
      bis =
          new Base64.InputStream(
              new java.io.BufferedInputStream(new java.io.FileInputStream(file)), Base64.ENCODE);

      // Read until done
      while ((numBytes = bis.read(buffer, length, 4096)) >= 0) length += numBytes;

      // Save in a variable to return
      encodedData = new String(buffer, 0, length, Base64.PREFERRED_ENCODING);

    } // end try
    catch (java.io.IOException e) {
      System.err.println("Error encoding from file " + filename);
    } // end catch: IOException
    finally {
      try {
        bis.close();
      } catch (Exception e) {
      }
    } // end finally

    return encodedData;
  } // end encodeFromFile
예제 #2
0
  /**
   * Convenience method for reading a base64-encoded file and decoding it.
   *
   * @param filename Filename for reading encoded data
   * @return decoded byte array or null if unsuccessful
   * @since 2.1
   */
  public static byte[] decodeFromFile(String filename) {
    byte[] decodedData = null;
    Base64.InputStream bis = null;
    try {
      // Set up some useful variables
      java.io.File file = new java.io.File(filename);
      byte[] buffer = null;
      int length = 0;
      int numBytes = 0;

      // Check for size of file
      if (file.length() > Integer.MAX_VALUE) {
        System.err.println(
            "File is too big for this convenience method (" + file.length() + " bytes).");
        return null;
      } // end if: file too big for int index
      buffer = new byte[(int) file.length()];

      // Open a stream
      bis =
          new Base64.InputStream(
              new java.io.BufferedInputStream(new java.io.FileInputStream(file)), Base64.DECODE);

      // Read until done
      while ((numBytes = bis.read(buffer, length, 4096)) >= 0) length += numBytes;

      // Save in a variable to return
      decodedData = new byte[length];
      System.arraycopy(buffer, 0, decodedData, 0, length);

    } // end try
    catch (java.io.IOException e) {
      System.err.println("Error decoding from file " + filename);
    } // end catch: IOException
    finally {
      try {
        bis.close();
      } catch (Exception e) {
      }
    } // end finally

    return decodedData;
  } // end decodeFromFile
예제 #3
0
  private void runStreamTest(int length) throws Exception {
    byte[] data = createData(length);
    ByteArrayOutputStream out_bytes = new ByteArrayOutputStream();
    OutputStream out = new Base64.OutputStream(out_bytes);
    out.write(data);
    out.close();
    byte[] encoded = out_bytes.toByteArray();
    byte[] decoded = Base64.decode(encoded, 0, encoded.length);
    assertTrue(Arrays.equals(data, decoded));

    Base64.InputStream in = new Base64.InputStream(new ByteArrayInputStream(encoded));
    out_bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[3];
    for (int n = in.read(buffer); n > 0; n = in.read(buffer)) {
      out_bytes.write(buffer, 0, n);
    }
    out_bytes.close();
    in.close();
    decoded = out_bytes.toByteArray();
    assertTrue(Arrays.equals(data, decoded));
  }