javabase64

javabase64 1.3 manual

javabase64 1.3 manual

Index

  1. Requirements
  2. Installation
  3. Javadocs
  4. Quickstart
  5. Encoding with Base64OutputStream
  6. Decoding with Base64InputStream

Requirements

To run the javabase64 library you need a Java Runtime Environment J2SE v. 1.2 or later.

Installation

Add the javabase64-1.3.jar file to your application classpath, and you'll be automatically enabled to the use of the javabase64 classes.

Javadocs

Here come the javabase64 javadocs .

Quickstart

The javabase64 library consists of two streams and a utility class.

The it.sauronsoftware.base64.Base64 utility class is, in most of cases, has everything you need to encode and decode.

First of all, you should tell apart if you have to encode/decode a string or a binary stream.

If you want to encode a string, you can easily call:

String encoded = Base64.encode("Hello, world!");

The result of this operation is a Base64 encoded string, that you can decode back later by calling:

String decoded = Base64.decode(encoded);

You can force the use of a specific charset during strings encoding and decoding operations:

String encoded = Base64.encode("Hello, world!", "UTF-8");
String decoded = Base64.decode(encoded, "UTF-8");

If you are working with binary data (i.e. images) you can use other forms of the encode() and decode() methods.

If data are not large and you handle them directly in memory, using a byte array:

byte[] source = ...; // load your data here
byte[] encoded = Base64.encode(source);
byte[] decoded = Base64.decode(encoded);

Larger data are better handled with streams. The next sample code encodes a file to another, using java.io.InputStream and java.io.OutputStream objects:

InputStream inputStream = new FileInputStream("source.jpg");
OutputStream outputStream = new FileOutputStream("encoded.b64");
Base64.encode(inputStream, outputStream);
outputStream.close();
inputStream.close();

In similar manner, a stream can be decoded:

InputStream inputStream = new FileInputStream("encoded.b64");
OutputStream outputStream = new FileOutputStream("decoded.jpg");
Base64.decode(inputStream, outputStream);
outputStream.close();
inputStream.close();

Shortcuts for java.io.File objects are provided. Encoding from a file to another:

File source = new File("source.jpg");
File encoded = new File("encoded.b64");
Base64.encode(source, encoded);

Decoding from a file to another:

File encoded = new File("encoded.b64");
File decoded = new File("decoded.jpg");
Base64.decode(encoded, decoded);

Encoding with Base64OutputStream

You can gain more control on the encoding process by avoiding the use of the static utilities in the Base64 class and by using directly a it.sauronsoftware.base64.Base64OutputStream instance.

The Base64OutputStream extends java.io.OutputStream and works as many other well know Java streams.

You can create a Base64OutputStream by wrapping another existing OutputStream :

Base64OutputStream encodingStream = new Base64OutputStream(myOutputStream);

I.e. you can send encoded data directly to a file by calling:

Base64OutputStream encodingStream = new Base64OutputStream(new FileOutputStream("encoded.b64"));

Now you can write your bytes in the stream as you would do with a common output stream. Data will be automatically encoded by the Base64OutputStream class.

encodingStream.write(bytes);

Don't forget to close the stream when the writing operation is completed. This is necessary to finalize the encoded data line.

encodingStream.close();

By default, Base64OutputStream wraps to a new line every 76 bytes sent to the underlying output stream. This behavior can be changed by supplying an alternative wrapAt value to the Base64OutputStream constructor:

Base64OutputStream encodingStream = new Base64OutputStream(myOutputStream, 48);

A value less than 1 disables wrapping:

Base64OutputStream encodingStream = new Base64OutputStream(myOutputStream, 0);

Decoding with Base64InputStream

You can gain more control on the decoding process by using a it.sauronsoftware.base64.Base64InputStream instance.

The Base64InputStream extends java.io.InputStream and works as many other well know Java streams.

You can create a Base64InputStream by wrapping another existing InputStream :

Base64InputStream decodingStream = new Base64InputStream(myInputStream);

I.e. you can read decoded data directly from an encoded file by calling:

Base64InputStream decodingStream = new Base64InputStream(new FileInputStream("encoded.b64"));

Now you can read your bytes from the stream as you would do with a common input stream. Data will be automatically decoded by the Base64InputStream class.

decodingStream.read(bytes);

Don't forget to close the stream when the reading operation is completed.

decodingStream.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Base64是一种常用的编码方法,用于将二进制数据表示为可打印字符。在Java中,有多种方式可以进行Base64的加密和解密操作。可以使用sun.misc.BASE64Encoder和sun.misc.BASE64Decoder类进行加密和解密操作,也可以使用com.sun.org.apache.xerces.internal.impl.dv.util.Base64类进行相同的操作。以下是使用这两种方式进行Base64编码和解码的示例代码: 方法一: ```java import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; public class Base64 { public static void main(String[] args) { System.out.println(Base64.getBase64Encode("你们好!!!")); System.out.println(Base64.getBase64Decode("5L2g5Lus5aW977yB77yB77yB")); } // 加密 public static String getBase64Encode(String str) { byte[] b = null; String s = null; try { b = str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (b != null) { s = new BASE64Encoder().encode(b); } return s; } // 解密 public static String getBase64Decode(String s) { byte[] b = null; String result = null; if (s != null) { BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(s); result = new String(b, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; } } ``` 方法二: ```java import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class Base64Demo { public static void main(String[] args) { // 加密 System.out.println(Base64Demo.getBase64Encode("Hello,World!")); // 解密 System.out.println(Base64Demo.getBase64Decode("SGVsbG9Xb3JsZA==")); } public static String getBase64Encode(String str) { if (str == null || "".equals(str)) { return ""; } try { byte[] bt = str.getBytes("UTF-8"); str = String.valueOf(Base64.encode(bt)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } public static String getBase64Decode(String str) { if (str == null || "".equals(str)) { return ""; } char[] ch = str.toCharArray(); byte[] bt = Base64.decode(String.valueOf(ch)); try { str = new String(bt,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值