Encode and decode integers, times, and internationalized strings to and from popular binary formats : Base64 « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Base64Screenshots 
Encode and decode integers, times, and internationalized strings to and from popular binary formats
    
/* encdec - encode and decode integers, times, and
 * internationalized strings to and from popular binary formats
 * http://www.ioplex.com/~miallen/encdec/
 * Copyright (c) 2003 Michael B. Allen <mballen@erols.com>
 *
 * The GNU Library General Public License
 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */


import java.util.Date;
import java.io.IOException;

public class Encdec {

    public static final long MILLISECONDS_BETWEEN_1970_AND_1601 = 11644473600000L;
    public static final long SEC_BETWEEEN_1904_AND_1970 = 2082844800L;
    public static final int TIME_1970_SEC_32BE = 1;
    public static final int TIME_1970_SEC_32LE = 2;
    public static final int TIME_1904_SEC_32BE = 3;
    public static final int TIME_1904_SEC_32LE = 4;
    public static final int TIME_1601_NANOS_64LE = 5;
    public static final int TIME_1601_NANOS_64BE = 6;
    public static final int TIME_1970_MILLIS_64BE = 7;
    public static final int TIME_1970_MILLIS_64LE = 8;

    /* Encode integers
     */

    public static int enc_uint16beshort s, byte[] dst, int di ) {
        dst[di++(byte)((s >> 80xFF);
        dst[di(byte)(s & 0xFF);
        return 2;
    }
    public static int enc_uint32beint i, byte[] dst, int di ) {
        dst[di++(byte)((i >> 240xFF);
        dst[di++(byte)((i >> 160xFF);
        dst[di++(byte)((i >> 80xFF);
        dst[di(byte)(i & 0xFF);
        return 4;
    }
    public static int enc_uint16leshort s, byte[] dst, int di )
    {
        dst[di++(byte)(s & 0xFF);
        dst[di(byte)((s >> 80xFF);
        return 2;
    }
    public static int enc_uint32leint i, byte[] dst, int di )
    {
        dst[di++(byte)(i & 0xFF);
        dst[di++(byte)((i >> 80xFF);
        dst[di++(byte)((i >> 160xFF);
        dst[di(byte)((i >> 240xFF);
        return 4;
    }

    /* Decode integers
     */

    public static short dec_uint16bebyte[] src, int si )
    {
        return (short)(((src[si0xFF<< 8(src[si + 10xFF));
    }
    public static int dec_uint32bebyte[] src, int si )
    {
        return ((src[si0xFF<< 24((src[si + 10xFF<< 16|
               ((src[si + 20xFF<< 8(src[si + 30xFF);
    }
    public static short dec_uint16lebyte[] src, int si )
    {
        return (short)((src[si0xFF((src[si + 10xFF<< 8));
    }
    public static int dec_uint32lebyte[] src, int si )
    {
        return (src[si0xFF((src[si + 10xFF<< 8|
               ((src[si + 20xFF<< 16((src[si + 30xFF<< 24);
    }

    /* Encode and decode 64 bit integers
     */

    public static int enc_uint64belong l, byte[] dst, int di )
    {
        enc_uint32be( (int)(l & 0xFFFFFFFFL), dst, di + );
        enc_uint32be( (int)(( l >> 32L 0xFFFFFFFFL), dst, di );
        return 8;
    }
    public static int enc_uint64lelong l, byte[] dst, int di )
    {
        enc_uint32le( (int)(l & 0xFFFFFFFFL), dst, di );
        enc_uint32le( (int)(( l >> 32L 0xFFFFFFFFL), dst, di + );
        return 8;
    }
    public static long dec_uint64bebyte[] src, int si )
    {
        long l;
        l = dec_uint32besrc, si 0xFFFFFFFFL;
        l <<= 32L;
        l |= dec_uint32besrc, si + 0xFFFFFFFFL;
        return l;
    }
    public static long dec_uint64lebyte[] src, int si )
    {
        long l;
        l = dec_uint32lesrc, si + 0xFFFFFFFFL;
        l <<= 32L;
        l |= dec_uint32lesrc, si 0xFFFFFFFFL;
        return l;
    }

    /* Encode floats
     */

    public static int enc_floatlefloat f, byte[] dst, int di )
    {
        return enc_uint32leFloat.floatToIntBits), dst, di );
    }
    public static int enc_floatbefloat f, byte[] dst, int di )
    {
        return enc_uint32beFloat.floatToIntBits), dst, di );
    }

    /* Decode floating point numbers
     */

    public static float dec_floatlebyte[] src, int si )
    {
        return Float.intBitsToFloatdec_uint32lesrc, si ));
    }
    public static float dec_floatbebyte[] src, int si )
    {
        return Float.intBitsToFloatdec_uint32besrc, si ));
    }

    /* Encode and decode doubles
     */

    public static int enc_doubleledouble d, byte[] dst, int di )
    {
        return enc_uint64leDouble.doubleToLongBits), dst, di );
    }
    public static int enc_doublebedouble d, byte[] dst, int di )
    {
        return enc_uint64beDouble.doubleToLongBits), dst, di );
    }
    public static double dec_doublelebyte[] src, int si )
    {
        return Double.longBitsToDoubledec_uint64lesrc, si ));
    }
    public static double dec_doublebebyte[] src, int si )
    {
        return Double.longBitsToDoubledec_uint64besrc, si ));
    }

    /* Encode times
     */

    public static int enc_timeDate date, byte[] dst, int di, int enc )
    {
        long t;

        switchenc ) {
            case TIME_1970_SEC_32BE:
                return enc_uint32be( (int)(date.getTime() 1000L), dst, di );
            case TIME_1970_SEC_32LE:
                return enc_uint32le( (int)(date.getTime() 1000L), dst, di );
            case TIME_1904_SEC_32BE:
                return enc_uint32be( (int)((date.getTime() 1000L +
                    SEC_BETWEEEN_1904_AND_19700xFFFFFFFF), dst, di );
            case TIME_1904_SEC_32LE:
                return enc_uint32le( (int)((date.getTime() 1000L +
                    SEC_BETWEEEN_1904_AND_19700xFFFFFFFF), dst, di );
            case TIME_1601_NANOS_64BE:
                t = (date.getTime() + MILLISECONDS_BETWEEN_1970_AND_160110000L;
                return enc_uint64bet, dst, di );
            case TIME_1601_NANOS_64LE:
                t = (date.getTime() + MILLISECONDS_BETWEEN_1970_AND_160110000L;
                return enc_uint64let, dst, di );
            case TIME_1970_MILLIS_64BE:
                return enc_uint64bedate.getTime(), dst, di );
            case TIME_1970_MILLIS_64LE:
                return enc_uint64ledate.getTime(), dst, di );
            default:
                throw new IllegalArgumentException"Unsupported time encoding" );
        }
    }

    /* Decode times
     */

    public static Date dec_timebyte[] src, int si, int enc )
    {
        long t;
    
        switchenc ) {
            case TIME_1970_SEC_32BE:
                return new Datedec_uint32besrc, si 1000L );
            case TIME_1970_SEC_32LE:
                return new Datedec_uint32lesrc, si 1000L );
            case TIME_1904_SEC_32BE:
                return new Date((( dec_uint32besrc, si 0xFFFFFFFFL-
                    SEC_BETWEEEN_1904_AND_1970 1000L );
            case TIME_1904_SEC_32LE:
                return new Date((( dec_uint32lesrc, si 0xFFFFFFFFL-
                    SEC_BETWEEEN_1904_AND_1970 1000L );
            case TIME_1601_NANOS_64BE:
                t = dec_uint64besrc, si );
                return new Datet / 10000L - MILLISECONDS_BETWEEN_1970_AND_1601);
            case TIME_1601_NANOS_64LE:
                t = dec_uint64lesrc, si );
                return new Datet / 10000L - MILLISECONDS_BETWEEN_1970_AND_1601);
            case TIME_1970_MILLIS_64BE:
                return new Datedec_uint64besrc, si ));
            case TIME_1970_MILLIS_64LE:
                return new Datedec_uint64lesrc, si ));
            default:
                throw new IllegalArgumentException"Unsupported time encoding" );
        }
    }

    public static int enc_utf8String str, byte[] dst, int di, int dlim throws IOException {
        int start = di, ch;
        int strlen = str.length();

        forint i = 0; di < dlim && i < strlen; i++ ) {
            ch = str.charAt);
            if ((ch >= 0x0001&& (ch <= 0x007F)) {
                dst[di++(byte)ch;
            else if (ch > 0x07FF) {
                if((dlim - di) {
                    break;
                }
                dst[di++(byte)(0xE0 ((ch >> 120x0F))
                dst[di++(byte)(0x80 ((ch >>  60x3F))
                dst[di++(byte)(0x80 ((ch >>  00x3F))
            else {
                if((dlim - di) {
                    break;
                }
                dst[di++(byte)(0xC0 ((ch >>  60x1F))
                dst[di++(byte)(0x80 ((ch >>  00x3F))
            }
        }

        return di - start;
    }
    public static String dec_utf8byte[] src, int si, int slim throws IOException {
        char[] uni = new char[slim - si];
        int ui, ch;

        forui = 0; si < slim && (ch = src[si++0xFF!= 0; ui++ ) {
            ifch < 0x80 ) {
                uni[ui(char)ch;
            else if((ch & 0xE0== 0xC0 ) {
                if((slim - si) {
                    break;
                }
                uni[ui(char)((ch & 0x1F<< 6);
                ch = src[si++0xFF;
                uni[ui|= ch & 0x3F;
                if ((ch & 0xC0!= 0x80 || uni[ui0x80 ) {
                    throw new IOException"Invalid UTF-8 sequence" );
                }
            else if((ch & 0xF0== 0xE0 ) {
                if((slim - si) {
                    break;
                }
                uni[ui]  (char)((ch & 0x0F<< 12);
                ch = src[si++0xFF;
                if ((ch & 0xC0!= 0x80 ) {
                    throw new IOException"Invalid UTF-8 sequence" );
                else {
                    uni[ui|= (ch & 0x3F<< 6;
                    ch = src[si++0xFF;
                    uni[ui|=  ch & 0x3F;
                    if ((ch & 0xC0!= 0x80 || uni[ui0x800) {
                        throw new IOException"Invalid UTF-8 sequence" );
                    }
                }
            else {
                throw new IOException"Unsupported UTF-8 sequence" );
            }
        }

        return new Stringuni, 0, ui );
    }
    public static String dec_ucs2lebyte[] src, int si, int slim, char[] buf throws IOException {
        int bi;

        forbi = 0(si + 1< slim; bi++, si += ) {
            buf[bi(char)dec_uint16lesrc, si );
            ifbuf[bi== '\0' ) {
                break;
            }
        }

        return new Stringbuf, 0, bi );
    }
}

   
    
    
    
  
Related examples in the same category
1. Base64 encoding/decoding.
2. Decodes Base64 data into octects
3. Implementation of MIME's Base64 encoding and decoding conversions.
4. Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5. Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6. Encodes and decodes to and from Base64 notation.
7. Encodes hex octects into Base64
8. Helper class to provide Base64 encoding routines.
9. Represents a collection of 64 boolean (on/off) flags.
10. byte to be tested if it is Base64 alphabet
11. to Base64
12. One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13. array of byte to encode
14. Codes number up to radix 62
15. A Base64 Encoder/Decoder
16. A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17. BASE64 encoder implementation
18. Base-64 Encoder - translates from base-64 text into binary
19. Base64 Character encoder as specified in RFC1113
20. Base64 Utils
21. Base64 encoder/decoder
22. Base64 from by Funambol, Inc.
23. Convert to hex from byte arrays and back
24. Converting hexadecimal strings
25. Encode and decode data in Base64 format as described in RFC 1521
26. Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
27. Performs Base64 encoding and/or decoding
28. Provides Base64 encoding and decoding as defined by RFC 2045
29. Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
30. Provides utility methods to Base64 encode data
31. QP Decoder Stream
32. QP Encoder Stream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.