3

Does anyone know about any other than com.idataconnect.lib.ascii85codec java projects that do something like org.apache.commons.codec.binary.Base64 class?

MatBanik
  • 26,356
  • 39
  • 116
  • 178

2 Answers2

5

I found this project that seems to do the trick: http://pdfbox.apache.org/downloads.html#recent

Following class encodes and decodes. Code reviews and suggestions are very welcome:

 import org.apache.pdfbox.io.ASCII85InputStream;
 import org.apache.pdfbox.io.ASCII85OutputStream;

 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;



 public class Ascii85Coder {

 public static byte[] decodeAscii85StringToBytes(String ascii85) {
    ArrayList<Byte> list = new ArrayList<Byte>();
    ByteArrayInputStream in_byte = null;
    try {
        in_byte = new ByteArrayInputStream(ascii85.getBytes("ascii"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    ASCII85InputStream in_ascii = new ASCII85InputStream(in_byte);
    try {
        int r ;
        while ((r = in_ascii.read()) != -1) {
            list.add((byte) r);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] bytes = new byte[list.size()];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = list.get(i);
    }
    return bytes;
}


public static String encodeBytesToAscii85(byte[] bytes) {
    ByteArrayOutputStream out_byte = new ByteArrayOutputStream();
    ASCII85OutputStream  out_ascii = new ASCII85OutputStream(out_byte);

    try {
        out_ascii.write(bytes);
        out_ascii.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String res = "";
    try {
        res = out_byte.toString("ascii");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return res;
}
}
MatBanik
  • 26,356
  • 39
  • 116
  • 178
  • Since PDFbox is open source you can copy the code to your project so as not to burden your classpath with things you may not need. – Pantelis Sopasakis Apr 16 '13 at 13:18
  • Suggestion: Remove printStackTrace. Either deal with the problem or re/throw an exception. – james.garriss Dec 17 '15 at 18:12
  • Note when copying open source source code into your project you still need to comply with the license of the copied code, e.g. give attribution, include copy of license etc. – Alwyn Schoeman Jan 26 '21 at 16:02
-1

There's Ascii85 implementation on java.net.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228