서비의 다락방   

JAVA Base64 Encoding / Decoding

JAVA 2007/10/12 20:44 by 서비
TAG ,

//Ok how about this pure Java API solution.
//Yours for free free free, no unintened theft.
//Completely portable per the Java promise.
//This code sets no preferences! The class just extends AbstractPreferences to get access to the put and get and over rides them.
//The super still calls the Base64 to do the encoding and decoding.
//This extension intercepts the encoded/decoded strings and makes them visible.
//TaDa Java API supports, has, allows, does base64 encoding, for the clever anyway.

package yourpackage;
public class YourPreferences extends java.util.prefs.AbstractPreferences {
//keep the key state
    private java.util.Hashtable encodedStore = new java.util.Hashtable();
   
    /** Creates a new instance of YourPreferences*/
    public YourPreferences(java.util.prefs.AbstractPreferences prefs, java.lang.String string) {
 super(prefs, string);
    }
//main acts as a driver i.e. used to test this class not required by this class
    public static void main(java.lang.String[] args ){
 java.util.prefs.AbstractPreferences myprefs = new yourpackage.YourPreferences(null, "");
 
 java.lang.String stringToEncode = "Aladdin:open sesame";
 java.lang.String key = "Aladdin:open sesame";
 java.lang.String key_ = "KEYisNOTtheSAMEasTHEstring";
 try{
     java.lang.String encoded = ((yourpackage.YourPreferences)myprefs).encodeBase64(stringToEncode);
     java.lang.System.out.println("ENCODED STRING TO: " + encoded);
     java.lang.String base64 = (java.lang.String)((yourpackage.YourPreferences)myprefs).encodedStore.get(key);
     java.lang.String decoded = ((yourpackage.YourPreferences)myprefs).decodeBase64("newkey",base64);
     java.lang.System.out.println("DECODED STRING TO: " + decoded);
    
     java.lang.String encoded_ = ((yourpackage.YourPreferences)myprefs).encodeBase64(key_,"ALONGSTRANGESTRINGTHATMAKESNOSENCEATALLBUTCANBEENCODEDANYWAYREGARDLESS");
     java.lang.System.out.println("ENCODED STRING TO: " + encoded_);
     java.lang.String base64_ = (java.lang.String)((yourpackage.YourPreferences)myprefs).encodedStore.get(key_);
     java.lang.String decoded_ = ((yourpackage.YourPreferences)myprefs).decodeBase64(key_,base64_);
     java.lang.System.out.println("DECODED STRING TO: " + decoded_);
     java.lang.System.out.println("\n");
     java.util.Enumeration enum = ((yourpackage.YourPreferences)myprefs).encodedStore.keys();
     java.lang.String enumKey = null;
     while(enum.hasMoreElements()){
  enumKey = (java.lang.String)enum.nextElement();
  java.lang.System.out.print(enumKey);
  java.lang.System.out.println(" : " +((yourpackage.YourPreferences)myprefs).encodedStore.get(enumKey).toString());
     }
 }catch(java.io.UnsupportedEncodingException uee){
     uee.printStackTrace();
 }catch(java.io.IOException ioe){
     ioe.printStackTrace();
 }
    }
    public java.lang.String encodeBase64(java.lang.String key, java.lang.String raw)throws java.io.UnsupportedEncodingException{
 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
 java.io.PrintWriter pw = new java.io.PrintWriter(
     new java.io.OutputStreamWriter(baos,"UTF8"));
 pw.write(raw.toCharArray());
 pw.flush();//ya know
 byte[] rawUTF8 = baos.toByteArray();
 this.putByteArray(key, rawUTF8);
 
 return (java.lang.String)this.encodedStore.get(key);
    }
    public java.lang.String encodeBase64(java.lang.String raw)throws java.io.UnsupportedEncodingException{
 
 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
 java.io.PrintWriter pw = new java.io.PrintWriter(
     new java.io.OutputStreamWriter(baos,"UTF8"));
 pw.write(raw.toCharArray());
 pw.flush();//ya know
 byte[] rawUTF8 = baos.toByteArray();
 this.putByteArray(raw, rawUTF8);
 
 return (java.lang.String)this.encodedStore.get(raw);
    }
    public java.lang.String decodeBase64(java.lang.String key, java.lang.String base64String)
    throws java.io.UnsupportedEncodingException, java.io.IOException{
 byte[] def = {(byte)'D',(byte)'E',(byte)'F'};//not used at any point
 this.encodedStore.put(key,base64String);
 char[] platformChars = null;
 byte[] byteResults = null;
 byteResults = this.getByteArray(key, def);
 java.io.InputStreamReader isr = new java.io.InputStreamReader(
     new java.io.ByteArrayInputStream(byteResults),"UTF8");
 platformChars = new char[byteResults.length];
 isr.read(platformChars);
 
 return new java.lang.String(platformChars);
    }
//intercept key lookup and return our own base64 encoded string to super
    public String get(String key, String def) {
 return (java.lang.String)this.encodedStore.get(key);
    }
//intercepts put captures the base64 encoded string and returns it
    public void put(String key, String value){
 this.encodedStore.put(key, value);//save the encoded string
    }
//dummy implementation as AbstractPreferences is extended to get acces to protected
//methods and to overide put(String,String) and get(String,String)
    protected java.util.prefs.AbstractPreferences childSpi(String name) {return null;}
    protected String[] childrenNamesSpi() throws java.util.prefs.BackingStoreException {return null;}
    protected void flushSpi() throws java.util.prefs.BackingStoreException {}
    protected String getSpi(String key) {return null;}
    protected String[] keysSpi() throws java.util.prefs.BackingStoreException {return null;}
    protected void putSpi(String key, String value) {}
    protected void removeNodeSpi() throws java.util.prefs.BackingStoreException {}
    protected void removeSpi(String key) {}
    protected void syncSpi() throws java.util.prefs.BackingStoreException {}
}

   

   
제 불찰로 2008년 1월 7일 이후 이곳에 새로운 포스팅을 올리지 않습니다.
http://www.yunsobi.com/blog에서 계속되는 서비의 이야기를 봐 주세요.

TRACKBACK :: http://www.yunsobi.com/tt/subby/trackback/251

댓글을 달아 주세요

  1. penis games  댓글주소  수정/삭제  댓글쓰기

    우수한 위치! 많은 감사.

    2008/05/23 04:37
  2. bouncy free tit  댓글주소  수정/삭제  댓글쓰기

    중대하고 유용한 위치!

    2008/05/23 04:56
  3. girl teens posing in bras  댓글주소  수정/삭제  댓글쓰기

    유용한 정보. 좋은 디자인.

    2008/05/23 05:41
  4. nudist photo gallery  댓글주소  수정/삭제  댓글쓰기

    너는 위치를차가운 만들었다!

    2008/05/23 06:04
  5. free porn wedding  댓글주소  수정/삭제  댓글쓰기

    여기 이것은 뉴스 있다!

    2008/05/23 07:14
  6. medicine bag  댓글주소  수정/삭제  댓글쓰기

    저에서 유사한 역사는 이었다.

    2008/05/23 07:38
  7. gay movie thai  댓글주소  수정/삭제  댓글쓰기

    좋은 너를 위치! 감사하십시요.

    2008/05/24 00:49
  8. gay in cornwall  댓글주소  수정/삭제  댓글쓰기

    우수한 디자인!!

    2008/05/24 00:49
  9. diy floor heating under  댓글주소  수정/삭제  댓글쓰기

    너는 위치를차가운 만들었다!

    2008/05/24 00:54
  10. culitos mojados  댓글주소  수정/삭제  댓글쓰기

    나는 배웠다 매우…

    2008/05/24 01:07
  11. anal blonde girl  댓글주소  수정/삭제  댓글쓰기

    많은 감사 위치! 우수한 나는 너의.

    2008/05/24 03:48
  12. adult contact direct  댓글주소  수정/삭제  댓글쓰기

    관심을 끌. 너가 좋을 동일할 지점을.

    2008/05/24 04:06

1  ... 37 38 39 40 41 42 43 44 45  ... 265 
«   2008/12   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31