`
pharaohsprince
  • 浏览: 295646 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

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();
分享到:
评论

相关推荐

    java Base64工具类

    java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类 java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类 java Base64工具类 java Base64工具类java Base64工具类 java ...

    javabase64-1.2.jar

    【标题】"javabase64-1.2.jar" 是一个Java库,主要用于实现Base64编码和解码功能。Base64是一种常见的数据编码方法,尤其在处理二进制数据时,如图片或证书,将其转换为ASCII文本格式以便在网络传输或存储时使用。这...

    javabase64-1.3.1

    JavaBase64是Java平台上的一个库,用于处理Base64编码和解码。Base64是一种用于在电子邮件、HTTP头和其他需要传输二进制数据的文本格式中表示二进制数据的编码方法。它将任何可打印的ASCII字符转换为一个64字符的...

    java Base64自定义码表

    在Java中,实现Base64编码和解码通常使用`java.util.Base64`类或Apache Commons Codec库中的`Base64`类。标准的Base64编码过程是将每3个字节的数据转换为4个6位的二进制数,然后将这些二进制数映射到编码表中的字符...

    javabase64-1.3.1.jar

    JavaBase64-1.3.1.jar 是一个专门用于Base64编码和解码的Java工具包。在计算机编程中,Base64是一种将任意二进制数据转换为可打印字符的编码方式,通常用于在网络上传输包含二进制的数据,如图片、音视频文件等。...

    Java Base64加密解密方法工具类

    Java Base64加密解密方法工具类

    java base64的mp3转base64的pcm

    java base64的MP3转base64的pcm工具类,适用于微信小程序的语音转文字,因为小程序的录音为MP3,大部分语音读写的第三方只支持wav和pcm,不用生成文件

    javabase64-1.3.1.rar

    JavaBase64是一个Java库,专门用于处理Base64编码和解码操作。Base64是一种数据编码方法,常用于在网络上传输二进制数据,因为HTTP和电子邮件等协议主要处理ASCII字符,而不直接支持二进制数据。Base64编码可以将...

    java Base64工具类 java Base64工具类

    java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类java Base64工具类 java Base64工具类java Base64工具类 java ...

    Java Base64文件加密解密类.rar

    Java Base64是一种在Java平台上实现的用于对二进制数据进行编码和解码的机制。Base64编码是网络上最常见的用于传输8位字节的编码方式之一,它将任意的字节序列转换为一系列可打印的字符,通常用于在电子邮件、URL、...

    javabase64-1.3.1.jar 依赖包

    javabase64-1.3.1.jar

    java base64的jar包

    `javabase64-1.3.1.jar` 是一个包含Base64编码和解码功能的Java库,它提供了一些关键类和方法供开发者使用。例如,主要类`Base64`包含了编码和解码的方法,如`encodeBytes()`用于编码字节数组,`decode()`用于解码...

    java base64源码+jar包

    Java中的Base64编码是一种将任意二进制数据转换为可打印ASCII字符的编码方式,广泛应用于网络传输、数据存储等领域。Base64编码的基本原理是将每3个字节(24位)的数据转化为4个6位的十六进制数字,然后用64个可打印...

    javabase64-1.2.zip

    Java Base64 这是一个用于编码和解码(encode/decode )base64字符串和数据流的Java开源类库。Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一。可用来作为电子邮件或WebService附件的传输编码.

    Java Base64支持encodeBase64String和decodeBase64String的包

    Base64工具类包,一般用于使用AES加密解密类中的使用工具类中需要引用的jar包

    JavaBase64Decoder

    JavaBase64Decoder是Java中处理Base64编码和解码的一个重要工具,它与BASE64Encoder一起工作,提供了一种将字节数组与Base64字符串之间的转换方式。Base64是一种用于在网络上传输二进制数据的编码方式,它将任意的...

    JAVA Base64算法

    JAVA Base64算法 对字符进行64位编码,解码

    javaBase64

    JavaBase64是Java平台上的一个库,专门用于处理Base64编码和解码操作。Base64是一种数据编码方式,常用于在不支持二进制传输的环境中传输文本数据,比如电子邮件系统或者HTML文档。它将二进制数据转换为可打印的...

Global site tag (gtag.js) - Google Analytics