Base64.java Tool Class

Source: Internet
Author: User
Tags base64

ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable; @SuppressWarnings ("Restriction") Public classBase64 {/*** STR encoded as base64 * *@paramString S *@returnString Base64 **/     Public Staticstring GetBase64 (string s) {if(s = =NULL)            return NULL; return(NewSun.misc.BASE64Encoder ()). Encode (S.getbytes ()); }    /*** byte[] encoded as base64 * *@parambyte[] BA *@returnString Base64 **/     Public StaticString GetBase64 (byte[] ba) {        if(BA = =NULL)            return NULL; return(NewSun.misc.BASE64Encoder ()). Encode (BA); }    /*** Base64 decoding for byte[] * *@paramString Base64 *@returnbyte[] *@throwsIOException **/     Public Static byte[] Base64tobytearray (String base64)throwsIOException {if(Base64 = =NULL)            return NULL; return(NewSun.misc.BASE64Decoder ()). Decodebuffer (base64); }        /*** Base64 string is deserialized to the specified class * *@paramBase64 string encoded with Base64 after serialization *@paramSerializedclass class * * For serialized Objects@returntyped object, can is null. * */@SuppressWarnings ("Unchecked")     Public Static<T> T deFromBase64 (String base64, class<t>Serializedclass) {Object o=NULL; byte[] ba =NULL; Bytearrayinputstream Bai=NULL; ObjectInputStream Oi=NULL; Try{BA=Base64.base64tobytearray (base64); Bai=NewBytearrayinputstream (BA); Oi=NewObjectInputStream (BAI); o=Oi.readobject (); } Catch(IOException e) {System.out.println ("Deserialization failed!" ");        E.printstacktrace (); } Catch(ClassNotFoundException e) {System.out.println ("Class not Found! ");        E.printstacktrace (); } finally {            Try {                if(Oi! =NULL) {oi.close (); }                if(Bai! =NULL) {bai.close (); }            } Catch(IOException e) {System.out.println ("Close Stream Exception!" ");            E.printstacktrace (); }        }        returno = =NULL?NULL: (T) o; }    /*** Serialize object to Base64 String * *@paramobj implements the object of the serializable interface *@returnString object is serialized as string after encoding to BASE64 **/     Public StaticString se2base64 (Serializable obj) {byte[] ba =NULL; Bytearrayoutputstream BAOs=NULL; ObjectOutputStream Oos=NULL; Try{BAOs=NewBytearrayoutputstream (); Oos=NewObjectOutputStream (BAOs);            Oos.writeobject (obj); BA=Baos.tobytearray (); } Catch(IOException e) {System.out.println ("Serialization failed!" "); } finally {            Try {                if(BAOs! =NULL) {baos.close (); }                if(Oos! =NULL) {oos.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }        returnBA = =NULL?NULL: Base64.getbase64 (BA); }}

Base64.java Tool Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.