OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 180
Line 33... Line 33...
33
 * consolidated and cleaner. The code now detects when data that's being decoded is gzip-compressed
33
 * consolidated and cleaner. The code now detects when data that's being decoded is gzip-compressed
34
 * and will decompress it automatically. Generally things are cleaner. You'll probably have to
34
 * and will decompress it automatically. Generally things are cleaner. You'll probably have to
35
 * change some method calls that you were making to support the new options format (<tt>int</tt>s
35
 * change some method calls that you were making to support the new options format (<tt>int</tt>s
36
 * that you "OR" together).</li>
36
 * that you "OR" together).</li>
37
 * <li>v1.5.1 - Fixed bug when decompressing and decoding to a byte[] using
37
 * <li>v1.5.1 - Fixed bug when decompressing and decoding to a byte[] using
38
 * <tt>decode( String s, boolean gzipCompressed )</tt>. Added the ability to "suspend" encoding
38
 * <tt>decode( String s, boolean gzipCompressed )</tt>. Added the ability to "suspend" encoding in
39
 * in the Output Stream so you can turn on and off the encoding if you need to embed base64 data in
39
 * the Output Stream so you can turn on and off the encoding if you need to embed base64 data in an
40
 * an otherwise "normal" stream (like an XML file).</li>
40
 * otherwise "normal" stream (like an XML file).</li>
41
 * <li>v1.5 - Output stream pases on flush() command but doesn't do anything itself. This helps
41
 * <li>v1.5 - Output stream pases on flush() command but doesn't do anything itself. This helps when
42
 * when using GZIP streams. Added the ability to GZip-compress objects before encoding them.</li>
42
 * using GZIP streams. Added the ability to GZip-compress objects before encoding them.</li>
43
 * <li>v1.4 - Added helper methods to read/write files.</li>
43
 * <li>v1.4 - Added helper methods to read/write files.</li>
44
 * <li>v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.</li>
44
 * <li>v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.</li>
45
 * <li>v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream where last
45
 * <li>v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream where last
46
 * buffer being read, if not completely full, was not returned.</li>
46
 * buffer being read, if not completely full, was not returned.</li>
47
 * <li>v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.</li>
47
 * <li>v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.</li>
48
 * <li>v1.3.3 - Fixed I/O streams which were totally messed up.</li>
48
 * <li>v1.3.3 - Fixed I/O streams which were totally messed up.</li>
49
 * </ul>
49
 * </ul>
50
 * 
50
 * 
51
 * <p>
51
 * <p>
52
 * I am placing this code in the Public Domain. Do with it as you will. This software comes with no
52
 * I am placing this code in the Public Domain. Do with it as you will. This software comes with no
53
 * guarantees or warranties but with plenty of well-wishing instead! Please visit <a
53
 * guarantees or warranties but with plenty of well-wishing instead! Please visit
54
 * href="http://iharder.net/base64">http://iharder.net/base64</a> periodically to check for updates
54
 * <a href="http://iharder.net/base64">http://iharder.net/base64</a> periodically to check for
55
 * or to contribute improvements.
55
 * updates or to contribute improvements.
56
 * </p>
56
 * </p>
57
 * 
57
 * 
58
 * @author Robert Harder
58
 * @author Robert Harder
59
 * @author rob@iharder.net
59
 * @author rob@iharder.net
60
 * @version 2.1
60
 * @version 2.1
Line 93... Line 93...
93
    private final static String PREFERRED_ENCODING = "UTF-8";
93
    private final static String PREFERRED_ENCODING = "UTF-8";
94
 
94
 
95
    /** The 64 valid Base64 values. */
95
    /** The 64 valid Base64 values. */
96
    private final static byte[] ALPHABET;
96
    private final static byte[] ALPHABET;
97
    private final static byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */
97
    private final static byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */
98
    { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
98
            { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
99
            (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e',
99
                    (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
100
            (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't',
100
                    (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
101
            (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',
101
                    (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
102
            (byte) '9', (byte) '+', (byte) '/' };
102
                    (byte) '8', (byte) '9', (byte) '+', (byte) '/' };
103
 
103
 
104
    /** Determine which ALPHABET to use. */
104
    /** Determine which ALPHABET to use. */
105
    static {
105
    static {
106
        byte[] __bytes;
106
        byte[] __bytes;
107
        try {
107
        try {
Line 165... Line 165...
165
 
165
 
166
    /**
166
    /**
167
     * Encodes up to the first three bytes of array <var>threeBytes</var> and returns a four-byte
167
     * Encodes up to the first three bytes of array <var>threeBytes</var> and returns a four-byte
168
     * array in Base64 notation. The actual number of significant bytes in your array is given by
168
     * array in Base64 notation. The actual number of significant bytes in your array is given by
169
     * <var>numSigBytes</var>. The array <var>threeBytes</var> needs only be as big as
169
     * <var>numSigBytes</var>. The array <var>threeBytes</var> needs only be as big as
170
     * <var>numSigBytes</var>. Code can reuse a byte array by passing a four-byte array as <var>b4</var>.
170
     * <var>numSigBytes</var>. Code can reuse a byte array by passing a four-byte array as
-
 
171
     * <var>b4</var>.
171
     * 
172
     * 
172
     * @param b4 A reusable byte array to reduce array instantiation
173
     * @param b4 A reusable byte array to reduce array instantiation
173
     * @param threeBytes the array to convert
174
     * @param threeBytes the array to convert
174
     * @param numSigBytes the number of significant bytes in your array
175
     * @param numSigBytes the number of significant bytes in your array
175
     * @return four byte array in Base64 notation.
176
     * @return four byte array in Base64 notation.
Line 179... Line 180...
179
        encode3to4(threeBytes, 0, numSigBytes, b4, 0);
180
        encode3to4(threeBytes, 0, numSigBytes, b4, 0);
180
        return b4;
181
        return b4;
181
    } // end encode3to4
182
    } // end encode3to4
182
 
183
 
183
    /**
184
    /**
184
     * Encodes up to three bytes of the array <var>source</var> and writes the resulting four
185
     * Encodes up to three bytes of the array <var>source</var> and writes the resulting four Base64
185
     * Base64 bytes to <var>destination</var>. The source and destination arrays can be manipulated
186
     * bytes to <var>destination</var>. The source and destination arrays can be manipulated
186
     * anywhere along their length by specifying <var>srcOffset</var> and <var>destOffset</var>.
187
     * anywhere along their length by specifying <var>srcOffset</var> and <var>destOffset</var>.
187
     * This method does not check to make sure your arrays are large enough to accomodate
188
     * This method does not check to make sure your arrays are large enough to accomodate
188
     * <var>srcOffset</var> + 3 for the <var>source</var> array or <var>destOffset</var> + 4 for
189
     * <var>srcOffset</var> + 3 for the <var>source</var> array or <var>destOffset</var> + 4 for the
189
     * the <var>destination</var> array. The actual number of significant bytes in your array is
190
     * <var>destination</var> array. The actual number of significant bytes in your array is given
190
     * given by <var>numSigBytes</var>.
191
     * by <var>numSigBytes</var>.
191
     * 
192
     * 
192
     * @param source the array to convert
193
     * @param source the array to convert
193
     * @param srcOffset the index where conversion begins
194
     * @param srcOffset the index where conversion begins
194
     * @param numSigBytes the number of significant bytes in your array
195
     * @param numSigBytes the number of significant bytes in your array
195
     * @param destination the array to hold the conversion
196
     * @param destination the array to hold the conversion
Line 340... Line 341...
340
     * 
341
     * 
341
     * @param source The data to convert
342
     * @param source The data to convert
342
     * @since 1.4
343
     * @since 1.4
343
     */
344
     */
344
    public static String encodeBytes(byte[] source) {
345
    public static String encodeBytes(byte[] source) {
345
        return encodeBytes(source, 0, source.length, NO_OPTIONS);
346
        return encodeBytes(source, 0, source.length, DONT_BREAK_LINES);
346
    } // end encodeBytes
347
    } // end encodeBytes
347
 
348
 
-
 
349
    public static String encodeBytesBreakLines(byte[] source) {
-
 
350
        return encodeBytes(source, NO_OPTIONS);
-
 
351
    }
-
 
352
 
348
    /**
353
    /**
349
     * Encodes a byte array into Base64 notation.
354
     * Encodes a byte array into Base64 notation.
350
     * <p>
355
     * <p>
351
     * Valid options:
356
     * Valid options:
352
     * 
357
     * 
Line 379... Line 384...
379
     * @param off Offset in array where conversion should begin
384
     * @param off Offset in array where conversion should begin
380
     * @param len Length of data to convert
385
     * @param len Length of data to convert
381
     * @since 1.4
386
     * @since 1.4
382
     */
387
     */
383
    public static String encodeBytes(byte[] source, int off, int len) {
388
    public static String encodeBytes(byte[] source, int off, int len) {
384
        return encodeBytes(source, off, len, NO_OPTIONS);
389
        return encodeBytes(source, off, len, DONT_BREAK_LINES);
385
    } // end encodeBytes
390
    } // end encodeBytes
386
 
391
 
-
 
392
    public static String encodeBytesBreakLines(byte[] source, int off, int len) {
-
 
393
        return encodeBytes(source, off, len, NO_OPTIONS);
-
 
394
    }
-
 
395
 
387
    /**
396
    /**
388
     * Encodes a byte array into Base64 notation.
397
     * Encodes a byte array into Base64 notation.
389
     * <p>
398
     * <p>
390
     * Valid options:
399
     * Valid options:
391
     * 
400
     * 
Line 503... Line 512...
503
    /**
512
    /**
504
     * Decodes four bytes from array <var>source</var> and writes the resulting bytes (up to three
513
     * Decodes four bytes from array <var>source</var> and writes the resulting bytes (up to three
505
     * of them) to <var>destination</var>. The source and destination arrays can be manipulated
514
     * of them) to <var>destination</var>. The source and destination arrays can be manipulated
506
     * anywhere along their length by specifying <var>srcOffset</var> and <var>destOffset</var>.
515
     * anywhere along their length by specifying <var>srcOffset</var> and <var>destOffset</var>.
507
     * This method does not check to make sure your arrays are large enough to accomodate
516
     * This method does not check to make sure your arrays are large enough to accomodate
508
     * <var>srcOffset</var> + 4 for the <var>source</var> array or <var>destOffset</var> + 3 for
517
     * <var>srcOffset</var> + 4 for the <var>source</var> array or <var>destOffset</var> + 3 for the
509
     * the <var>destination</var> array. This method returns the actual number of bytes that were
518
     * <var>destination</var> array. This method returns the actual number of bytes that were
510
     * converted from the Base64 encoding.
519
     * converted from the Base64 encoding.
511
     * 
520
     * 
512
     * 
521
     * 
513
     * @param source the array to convert
522
     * @param source the array to convert
514
     * @param srcOffset the index where conversion begins
523
     * @param srcOffset the index where conversion begins
Line 692... Line 701...
692
 
701
 
693
        return bytes;
702
        return bytes;
694
    } // end decode
703
    } // end decode
695
 
704
 
696
    /**
705
    /**
697
     * Attempts to decode Base64 data and deserialize a Java Object within. Returns <tt>null</tt>
706
     * Attempts to decode Base64 data and deserialize a Java Object within. Returns <tt>null</tt> if
698
     * if there was an error.
707
     * there was an error.
699
     * 
708
     * 
700
     * @param encodedObject The Base64 data to decode
709
     * @param encodedObject The Base64 data to decode
701
     * @return The decoded and deserialized object
710
     * @return The decoded and deserialized object
702
     * @since 1.5
711
     * @since 1.5
703
     */
712
     */
Line 907... Line 916...
907
    } // end encodeFromFile
916
    } // end encodeFromFile
908
 
917
 
909
    /* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
918
    /* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
910
 
919
 
911
    /**
920
    /**
912
     * A {@link Base64.InputStream} will read data from another <tt>java.io.InputStream</tt>,
921
     * A {@link Base64.InputStream} will read data from another <tt>java.io.InputStream</tt>, given
913
     * given in the constructor, and encode/decode to/from Base64 notation on the fly.
922
     * in the constructor, and encode/decode to/from Base64 notation on the fly.
914
     * 
923
     * 
915
     * @see Base64
924
     * @see Base64
916
     * @since 1.3
925
     * @since 1.3
917
     */
926
     */
918
    public static class InputStream extends java.io.FilterInputStream {
927
    public static class InputStream extends java.io.FilterInputStream {
Line 1105... Line 1114...
1105
    } // end inner class InputStream
1114
    } // end inner class InputStream
1106
 
1115
 
1107
    /* ******** I N N E R C L A S S O U T P U T S T R E A M ******** */
1116
    /* ******** I N N E R C L A S S O U T P U T S T R E A M ******** */
1108
 
1117
 
1109
    /**
1118
    /**
1110
     * A {@link Base64.OutputStream} will write data to another <tt>java.io.OutputStream</tt>,
1119
     * A {@link Base64.OutputStream} will write data to another <tt>java.io.OutputStream</tt>, given
1111
     * given in the constructor, and encode/decode to/from Base64 notation on the fly.
1120
     * in the constructor, and encode/decode to/from Base64 notation on the fly.
1112
     * 
1121
     * 
1113
     * @see Base64
1122
     * @see Base64
1114
     * @since 1.3
1123
     * @since 1.3
1115
     */
1124
     */
1116
    public static class OutputStream extends java.io.FilterOutputStream {
1125
    public static class OutputStream extends java.io.FilterOutputStream {