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