Base64 程式庫

piBase64 程式庫

package com.PowerIntegral;

import sun.misc.*;

import java.io.*;

import java.util.*;

/**

* <p>Title: piBase64</p>

*

* <p>Description: piBase64</p>

*/

public class piBase64 {

private String _ErrMessage = "";

private boolean _OK = false;

private String _defaultEncoding = System.getProperty("file.encoding");

/**

* piBase64: 建構子

*/

public piBase64() {

}

/**

* piBase64: 建構子

*

* @param defaultEncoding String => 預設字碼

*/

public piBase64(String defaultEncoding) {

this._defaultEncoding = defaultEncoding;

}

/**

* piBase64: 建構子

*

* @param Configuration Properties => 定義物件

*/

public piBase64(Properties Configuration) {

this._defaultEncoding = Configuration.getProperty(

"DefaultBase64Encoding");

if (this._defaultEncoding == null || this._defaultEncoding.equals("")) {

this._defaultEncoding = System.getProperty("file.encoding");

}

}

/**

* getEncodedString: 轉換字碼

*

* @param sourceString String => 來源字串

* @param defaultEncoding String => 預設字碼

* @return String

*/

public String getEncodedString(String sourceString, String defaultEncoding) {

String _targetString = "";

byte[] _targetBytes;

try {

_targetBytes = sourceString.getBytes(this._defaultEncoding);

_targetString = new String(_targetBytes, defaultEncoding);

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

}

return _targetString;

}

/**

* setDefaultEncodingString: 設定預設字碼

*

* @param defaultEncoding String => 預設字碼

*/

public void setDefaultEncodingString(String defaultEncoding) {

this._defaultEncoding = defaultEncoding;

}

/**

* getDefaultEncodingString: 取得預設字碼

*

* @return String

*/

public String getDefaultEncodingString() {

return this._defaultEncoding;

}

/**

* _Convert: Base64 編碼間轉換

*

* @param sourceString String => 來源字串

* @param toBase64 boolean => 編碼/解碼

* @return String

*/

private String _Convert(String sourceString, boolean toBase64) {

String _targetString = "";

if (toBase64) {

_targetString = piString.ReplaceAll(sourceString,

System.

getProperty("line.separator"),

" ");

} else {

_targetString = piString.ReplaceAll(sourceString, " ",

System.getProperty(

"line.separator"));

}

return _targetString;

}

/**

* getBase64: Base64 編碼

*

* @param sourceString String => 來源字串

* @return String

*/

public String getBase64(String sourceString) {

this._OK = false;

this._ErrMessage = "";

if (sourceString == null || sourceString == "") {

this._ErrMessage = "-1";

return "";

}

byte[] _sourceBytes = null;

String _ResultTemp = "";

String _targetString = "";

try {

_sourceBytes = sourceString.getBytes(this._defaultEncoding);

sun.misc.BASE64Encoder _BASE64Encoder = new sun.misc.BASE64Encoder();

_ResultTemp = _BASE64Encoder.encodeBuffer(_sourceBytes);

_targetString = this._Convert(_ResultTemp, true);

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

}

return _targetString;

}

/**

* getFromBase64: Base64 解碼

*

* @param sourceBase64String String: 來源 Base64 字串

* @return String

*/

public String getFromBase64(String sourceBase64String) {

this._OK = false;

this._ErrMessage = "";

if (sourceBase64String == null || sourceBase64String == "") {

this._ErrMessage = "-1";

return "";

}

String _sourceBase64String = this._Convert(sourceBase64String, false);

sun.misc.BASE64Decoder _BASE64Decoder = new sun.misc.BASE64Decoder();

String _targetString = "";

try {

byte[] _sourceBase64Bytes = _BASE64Decoder.decodeBuffer(

_sourceBase64String);

_targetString = new String(_sourceBase64Bytes,

this._defaultEncoding);

this._OK = true;

} catch (Exception ex) {

ex.printStackTrace(System.out);

this._ErrMessage = ex.getMessage();

}

return _targetString;

}

/**

* getObjectBase64: Base64 編碼

*

* @param sourceObject Serializable => 來源物件

* @return String

*/

public String getObjectBase64(Serializable sourceObject) {

this._OK = false;

this._ErrMessage = "";

if (sourceObject == null) {

this._ErrMessage = "-1";

return null;

}

byte[] _sourceBytes = null;

String _targetString = "";

try {

ByteArrayOutputStream _byteStream = new ByteArrayOutputStream();

ObjectOutputStream _objectStream = new ObjectOutputStream(

_byteStream);

try {

_objectStream.writeObject(sourceObject);

_objectStream.flush();

_sourceBytes = _byteStream.toByteArray();

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

return null;

}

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

return null;

}

_targetString = this._Convert((new sun.misc.BASE64Encoder()).encode(

_sourceBytes), true);

return _targetString;

}

/**

* getObjectFromBase64: Base64 解碼

*

* @param sourceBase64String String => 來源 Base64 字串

* @return Serializable

*/

public Serializable getObjectFromBase64(String sourceBase64String) {

this._OK = false;

this._ErrMessage = "";

if (sourceBase64String == null) {

this._ErrMessage = "-1";

return null;

}

String _sourceBase64String = this._Convert(sourceBase64String, false);

BASE64Decoder _decoder = new BASE64Decoder();

Serializable _targetObject = null;

try {

byte[] sourceBase64Bytes = _decoder.decodeBuffer(

_sourceBase64String);

ByteArrayInputStream byteStream = new ByteArrayInputStream(

sourceBase64Bytes);

ObjectInputStream objectStream = new ObjectInputStream(byteStream);

_targetObject = (Serializable) objectStream.readObject();

this._OK = true;

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

}

return _targetObject;

}

/**

* getBytesBase64: Base64 編碼

*

* @param sourceBytes byte[] => 來源位元組

* @return String

*/

public String getBytesBase64(byte[] sourceBytes) {

this._OK = false;

this._ErrMessage = "";

if (sourceBytes == null) {

this._ErrMessage = "-1";

return null;

}

String _targetString = this._Convert((new sun.misc.BASE64Encoder()).

encode(sourceBytes), true);

return _targetString;

}

/**

* getBytesFromBase64: Base64 解碼

*

* @param sourceBase64String String => 來源 Base64 字串

* @return byte[]

*/

public byte[] getBytesFromBase64(String sourceBase64String) {

this._OK = false;

this._ErrMessage = "";

if (sourceBase64String == null || sourceBase64String == "") {

this._ErrMessage = "-1";

return null;

}

String _sourceBase64String = this._Convert(sourceBase64String, false);

BASE64Decoder _decoder = new BASE64Decoder();

byte[] _targetBase64Bytes = null;

try {

_targetBase64Bytes = _decoder.decodeBuffer(_sourceBase64String);

this._OK = true;

} catch (Exception ex) {

this._ErrMessage = ex.getMessage();

}

return _targetBase64Bytes;

}

/**

* getErrorMessage: 取得最近執行錯誤訊息

*

* @return String

*/

public String getErrorMessage() {

return this._ErrMessage;

}

/**

* getSuccess: 取得最近執行狀態

*

* @return boolean

*/

public boolean getSuccess() {

return this._OK;

}

}