/** * Decodes a base64 String. * * @param data a base64 encoded String to decode. * @return the decoded String. */ public static byte[] decodeBase64(String data) { return Base64.decode(data); }
/** * Encodes a byte array into a bse64 String. * * @param data The byte arry to encode. * @param offset the offset of the bytearray to begin encoding at. * @param len the length of bytes to encode. * @param lineBreaks True if the encoding should contain line breaks and false if it should not. * @return A base64 encoded String. */ public static String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks) { return Base64.encodeBytes(data, offset, len, (lineBreaks ? Base64.NO_OPTIONS : Base64.DONT_BREAK_LINES)); }
/** * Encodes or decodes two files from the command line; * <strong>feel free to delete this method (in fact you probably should) * if you're embedding this code into a larger program.</strong> */ public final static void main( String[] args ) { if( args.length < 3 ){ usage("Not enough arguments."); } // end if: args.length < 3 else { String flag = args[0]; String infile = args[1]; String outfile = args[2]; if( flag.equals( "-e" ) ){ Base64.encodeFileToFile( infile, outfile ); } // end if: encode else if( flag.equals( "-d" ) ) { Base64.decodeFileToFile( infile, outfile ); } // end else if: decode else { usage( "Unknown flag: " + flag ); } // end else } // end else } // end main
public void challengeReceived(String challenge) throws IOException { byte response[] = null; if (challenge != null) { String decodedResponse = new String(Base64.decode(challenge)); Map<String, String> parameters = getQueryMap(decodedResponse); Long callId = new GregorianCalendar().getTimeInMillis() / 1000; Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);
byte[] DECODABET = getDecodabet( options ); if( b4Posn > 3 ) outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options ); b4Posn = 0;
/** * Encodes up to the first three bytes of array <var>threeBytes</var> * and returns a four-byte array in Base64 notation. * The actual number of significant bytes in your array is * given by <var>numSigBytes</var>. * The array <var>threeBytes</var> needs only be as big as * <var>numSigBytes</var>. * Code can reuse a byte array by passing a four-byte array as <var>b4</var>. * * @param b4 A reusable byte array to reduce array instantiation * @param threeBytes the array to convert * @param numSigBytes the number of significant bytes in your array * @return four byte array in Base64 notation. * @since 1.5.1 */ private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes, int options ) { encode3to4( threeBytes, 0, numSigBytes, b4, 0, options ); return b4; } // end encode3to4
/** * Serializes an object and returns the Base64-encoded * version of that serialized object. If the object * cannot be serialized or there is another error, * the method will return <tt>null</tt>. * The object is not GZip-compressed before being encoded. * * @param serializableObject The object to encode * @return The Base64-encoded object * @since 1.4 */ public static String encodeObject( java.io.Serializable serializableObject ) { return encodeObject( serializableObject, NO_OPTIONS ); } // end encodeObject
/** * Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>. * * @param infile Input file * @param outfile Output file * @since 2.2 */ public static void decodeFileToFile( String infile, String outfile ) { byte[] decoded = Base64.decodeFromFile( infile ); java.io.OutputStream out = null; try{ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) ); out.write( decoded ); } // end try catch( java.io.IOException ex ) { ex.printStackTrace(); } // end catch finally { try { out.close(); } catch( Exception ex ){} } // end finally } // end decodeFileToFile
/** * Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>. * * @param infile Input file * @param outfile Output file * @since 2.2 */ public static void encodeFileToFile( String infile, String outfile ) { String encoded = Base64.encodeFromFile( infile ); java.io.OutputStream out = null; try{ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) ); out.write( encoded.getBytes("US-ASCII") ); // Strict, 7-bit output. } // end try catch( java.io.IOException ex ) { ex.printStackTrace(); } // end catch finally { try { out.close(); } catch( Exception ex ){} } // end finally } // end encodeFileToFile
byte[] destination, int destOffset, int options ) byte[] ALPHABET = getAlphabet( options );
String decodedResponse = new String(Base64.decode(challenge)); Map<String, String> parameters = getQueryMap(decodedResponse); authenticationText = Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);
byte[] DECODABET = getDecodabet( options ); if( b4Posn > 3 ) outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options ); b4Posn = 0;
/** * Encodes up to the first three bytes of array <var>threeBytes</var> * and returns a four-byte array in Base64 notation. * The actual number of significant bytes in your array is * given by <var>numSigBytes</var>. * The array <var>threeBytes</var> needs only be as big as * <var>numSigBytes</var>. * Code can reuse a byte array by passing a four-byte array as <var>b4</var>. * * @param b4 A reusable byte array to reduce array instantiation * @param threeBytes the array to convert * @param numSigBytes the number of significant bytes in your array * @return four byte array in Base64 notation. * @since 1.5.1 */ private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes, int options ) { encode3to4( threeBytes, 0, numSigBytes, b4, 0, options ); return b4; } // end encode3to4
/** * Serializes an object and returns the Base64-encoded * version of that serialized object. If the object * cannot be serialized or there is another error, * the method will return <tt>null</tt>. * The object is not GZip-compressed before being encoded. * * @param serializableObject The object to encode * @return The Base64-encoded object * @since 1.4 */ public static String encodeObject( java.io.Serializable serializableObject ) { return encodeObject( serializableObject, NO_OPTIONS ); } // end encodeObject
/** * Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>. * * @param infile Input file * @param outfile Output file * @since 2.2 */ public static void decodeFileToFile( String infile, String outfile ) { byte[] decoded = Base64.decodeFromFile( infile ); java.io.OutputStream out = null; try{ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) ); out.write( decoded ); } // end try catch( java.io.IOException ex ) { ex.printStackTrace(); } // end catch finally { try { out.close(); } catch( Exception ex ){} } // end finally } // end decodeFileToFile
/** * Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>. * * @param infile Input file * @param outfile Output file * @since 2.2 */ public static void encodeFileToFile( String infile, String outfile ) { String encoded = Base64.encodeFromFile( infile ); java.io.OutputStream out = null; try{ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) ); out.write( encoded.getBytes("US-ASCII") ); // Strict, 7-bit output. } // end try catch( java.io.IOException ex ) { ex.printStackTrace(); } // end catch finally { try { out.close(); } catch( Exception ex ){} } // end finally } // end encodeFileToFile
byte[] destination, int destOffset, int options ) byte[] ALPHABET = getAlphabet( options );
/** * Encodes a byte array into a bse64 String. * * @param data The byte arry to encode. * @param offset the offset of the bytearray to begin encoding at. * @param len the length of bytes to encode. * @param lineBreaks True if the encoding should contain line breaks and false if it should not. * @return A base64 encoded String. */ public static String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks) { return Base64.encodeBytes(data, offset, len, (lineBreaks ? Base64.NO_OPTIONS : Base64.DONT_BREAK_LINES)); }
/** * The server is challenging the SASL mechanism for the stanza he just sent. Send a * response to the server's challenge. * * @param challenge a base64 encoded string representing the challenge. * @throws IOException if an exception sending the response occurs. */ public void challengeReceived(String challenge) throws IOException { byte response[]; if(challenge != null) { response = sc.evaluateChallenge(Base64.decode(challenge)); } else { response = sc.evaluateChallenge(new byte[0]); } Packet responseStanza; if (response == null) { responseStanza = new Response(); } else { responseStanza = new Response(Base64.encodeBytes(response,Base64.DONT_BREAK_LINES)); } // Send the authentication to the server getSASLAuthentication().send(responseStanza); }
/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode * @return the decoded data * @since 1.4 */ public static byte[] decode( String s ) { return decode( s, NO_OPTIONS ); }