KEYCLOAK-5859
This commit is contained in:
parent
f923211e9f
commit
bc28223a77
2 changed files with 0 additions and 395 deletions
|
@ -1390,251 +1390,8 @@ public class Base64
|
|||
return obj;
|
||||
} // end decodeObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for encoding data to a file.
|
||||
*
|
||||
* <p>As of v 2.3, if there is a error,
|
||||
* the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
|
||||
* In earlier versions, it just returned false, but
|
||||
* in retrospect that's a pretty poor way to handle it.</p>
|
||||
*
|
||||
* @param dataToEncode byte array of data to encode in base64 form
|
||||
* @param filename Filename for saving encoded data
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @throws NullPointerException if dataToEncode is null
|
||||
* @since 2.1
|
||||
*/
|
||||
public static void encodeToFile( byte[] dataToEncode, String filename )
|
||||
throws java.io.IOException {
|
||||
|
||||
if( dataToEncode == null ){
|
||||
throw new NullPointerException( "Data to encode was null." );
|
||||
} // end iff
|
||||
|
||||
Base64.OutputStream bos = null;
|
||||
try {
|
||||
bos = new Base64.OutputStream(
|
||||
new java.io.FileOutputStream( filename ), Base64.ENCODE );
|
||||
bos.write( dataToEncode );
|
||||
} // end try
|
||||
catch( java.io.IOException e ) {
|
||||
throw e; // Catch and throw to execute finally{} block
|
||||
} // end catch: java.io.IOException
|
||||
finally {
|
||||
try{ bos.close(); } catch( Exception e ){}
|
||||
} // end finally
|
||||
|
||||
} // end encodeToFile
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for decoding data to a file.
|
||||
*
|
||||
* <p>As of v 2.3, if there is a error,
|
||||
* the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
|
||||
* In earlier versions, it just returned false, but
|
||||
* in retrospect that's a pretty poor way to handle it.</p>
|
||||
*
|
||||
* @param dataToDecode Base64-encoded data as a string
|
||||
* @param filename Filename for saving decoded data
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @since 2.1
|
||||
*/
|
||||
public static void decodeToFile( String dataToDecode, String filename )
|
||||
throws java.io.IOException {
|
||||
|
||||
Base64.OutputStream bos = null;
|
||||
try{
|
||||
bos = new Base64.OutputStream(
|
||||
new java.io.FileOutputStream( filename ), Base64.DECODE );
|
||||
bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
|
||||
} // end try
|
||||
catch( java.io.IOException e ) {
|
||||
throw e; // Catch and throw to execute finally{} block
|
||||
} // end catch: java.io.IOException
|
||||
finally {
|
||||
try{ bos.close(); } catch( Exception e ){}
|
||||
} // end finally
|
||||
|
||||
} // end decodeToFile
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for reading a base64-encoded
|
||||
* file and decoding it.
|
||||
*
|
||||
* <p>As of v 2.3, if there is a error,
|
||||
* the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
|
||||
* In earlier versions, it just returned false, but
|
||||
* in retrospect that's a pretty poor way to handle it.</p>
|
||||
*
|
||||
* @param filename Filename for reading encoded data
|
||||
* @return decoded byte array
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @since 2.1
|
||||
*/
|
||||
public static byte[] decodeFromFile( String filename )
|
||||
throws java.io.IOException {
|
||||
|
||||
byte[] decodedData = null;
|
||||
Base64.InputStream bis = null;
|
||||
try
|
||||
{
|
||||
// Set up some useful variables
|
||||
java.io.File file = new java.io.File( filename );
|
||||
byte[] buffer = null;
|
||||
int length = 0;
|
||||
int numBytes = 0;
|
||||
|
||||
// Check for size of file
|
||||
if( file.length() > Integer.MAX_VALUE )
|
||||
{
|
||||
throw new java.io.IOException( "File is too big for this convenience method (" + file.length() + " bytes)." );
|
||||
} // end if: file too big for int index
|
||||
buffer = new byte[ (int)file.length() ];
|
||||
|
||||
// Open a stream
|
||||
bis = new Base64.InputStream(
|
||||
new java.io.BufferedInputStream(
|
||||
new java.io.FileInputStream( file ) ), Base64.DECODE );
|
||||
|
||||
// Read until done
|
||||
while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
|
||||
length += numBytes;
|
||||
} // end while
|
||||
|
||||
// Save in a variable to return
|
||||
decodedData = new byte[ length ];
|
||||
System.arraycopy( buffer, 0, decodedData, 0, length );
|
||||
|
||||
} // end try
|
||||
catch( java.io.IOException e ) {
|
||||
throw e; // Catch and release to execute finally{}
|
||||
} // end catch: java.io.IOException
|
||||
finally {
|
||||
try{ bis.close(); } catch( Exception e) {}
|
||||
} // end finally
|
||||
|
||||
return decodedData;
|
||||
} // end decodeFromFile
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for reading a binary file
|
||||
* and base64-encoding it.
|
||||
*
|
||||
* <p>As of v 2.3, if there is a error,
|
||||
* the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
|
||||
* In earlier versions, it just returned false, but
|
||||
* in retrospect that's a pretty poor way to handle it.</p>
|
||||
*
|
||||
* @param filename Filename for reading binary data
|
||||
* @return base64-encoded string
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @since 2.1
|
||||
*/
|
||||
public static String encodeFromFile( String filename )
|
||||
throws java.io.IOException {
|
||||
|
||||
String encodedData = null;
|
||||
Base64.InputStream bis = null;
|
||||
try
|
||||
{
|
||||
// Set up some useful variables
|
||||
java.io.File file = new java.io.File( filename );
|
||||
byte[] buffer = new byte[ Math.max((int)(file.length() * 1.4+1),40) ]; // Need max() for math on small files (v2.2.1); Need +1 for a few corner cases (v2.3.5)
|
||||
int length = 0;
|
||||
int numBytes = 0;
|
||||
|
||||
// Open a stream
|
||||
bis = new Base64.InputStream(
|
||||
new java.io.BufferedInputStream(
|
||||
new java.io.FileInputStream( file ) ), Base64.ENCODE );
|
||||
|
||||
// Read until done
|
||||
while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
|
||||
length += numBytes;
|
||||
} // end while
|
||||
|
||||
// Save in a variable to return
|
||||
encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
|
||||
|
||||
} // end try
|
||||
catch( java.io.IOException e ) {
|
||||
throw e; // Catch and release to execute finally{}
|
||||
} // end catch: java.io.IOException
|
||||
finally {
|
||||
try{ bis.close(); } catch( Exception e) {}
|
||||
} // end finally
|
||||
|
||||
return encodedData;
|
||||
} // end encodeFromFile
|
||||
|
||||
/**
|
||||
* Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.
|
||||
*
|
||||
* @param infile Input file
|
||||
* @param outfile Output file
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @since 2.2
|
||||
*/
|
||||
public static void encodeFileToFile( String infile, String outfile )
|
||||
throws java.io.IOException {
|
||||
|
||||
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 e ) {
|
||||
throw e; // Catch and release to execute finally{}
|
||||
} // end catch
|
||||
finally {
|
||||
try { out.close(); }
|
||||
catch( Exception ex ){}
|
||||
} // end finally
|
||||
} // end encodeFileToFile
|
||||
|
||||
|
||||
/**
|
||||
* Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>.
|
||||
*
|
||||
* @param infile Input file
|
||||
* @param outfile Output file
|
||||
* @throws java.io.IOException if there is an error
|
||||
* @since 2.2
|
||||
*/
|
||||
public static void decodeFileToFile( String infile, String outfile )
|
||||
throws java.io.IOException {
|
||||
|
||||
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 e ) {
|
||||
throw e; // Catch and release to execute finally{}
|
||||
} // end catch
|
||||
finally {
|
||||
try { out.close(); }
|
||||
catch( Exception ex ){}
|
||||
} // end finally
|
||||
} // end decodeFileToFile
|
||||
|
||||
|
||||
/* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A {@link Base64.InputStream} will read data from another
|
||||
* <tt>java.io.InputStream</tt>, given in the constructor,
|
||||
|
|
|
@ -736,158 +736,6 @@ public class Base64 {
|
|||
return obj;
|
||||
} // end decodeObject
|
||||
|
||||
/**
|
||||
* Convenience method for encoding data to a file.
|
||||
*
|
||||
* @param dataToEncode byte array of data to encode in base64 form
|
||||
* @param filename Filename for saving encoded data
|
||||
* @return <tt>true</tt> if successful, <tt>false</tt> otherwise
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean encodeToFile(byte[] dataToEncode, String filename) {
|
||||
boolean success = false;
|
||||
Base64.OutputStream bos = null;
|
||||
try {
|
||||
bos = new Base64.OutputStream(new java.io.FileOutputStream(filename), Base64.ENCODE);
|
||||
bos.write(dataToEncode);
|
||||
success = true;
|
||||
} // end try
|
||||
catch (java.io.IOException e) {
|
||||
|
||||
success = false;
|
||||
} // end catch: IOException
|
||||
finally {
|
||||
try {
|
||||
bos.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} // end finally
|
||||
|
||||
return success;
|
||||
} // end encodeToFile
|
||||
|
||||
/**
|
||||
* Convenience method for decoding data to a file.
|
||||
*
|
||||
* @param dataToDecode Base64-encoded data as a string
|
||||
* @param filename Filename for saving decoded data
|
||||
* @return <tt>true</tt> if successful, <tt>false</tt> otherwise
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean decodeToFile(String dataToDecode, String filename) {
|
||||
boolean success = false;
|
||||
Base64.OutputStream bos = null;
|
||||
try {
|
||||
bos = new Base64.OutputStream(new java.io.FileOutputStream(filename), Base64.DECODE);
|
||||
bos.write(dataToDecode.getBytes(PREFERRED_ENCODING));
|
||||
success = true;
|
||||
} // end try
|
||||
catch (java.io.IOException e) {
|
||||
success = false;
|
||||
} // end catch: IOException
|
||||
finally {
|
||||
try {
|
||||
bos.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} // end finally
|
||||
|
||||
return success;
|
||||
} // end decodeToFile
|
||||
|
||||
/**
|
||||
* Convenience method for reading a base64-encoded file and decoding it.
|
||||
*
|
||||
* @param filename Filename for reading encoded data
|
||||
* @return decoded byte array or null if unsuccessful
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public static byte[] decodeFromFile(String filename) {
|
||||
byte[] decodedData = null;
|
||||
Base64.InputStream bis = null;
|
||||
try {
|
||||
// Set up some useful variables
|
||||
java.io.File file = new java.io.File(filename);
|
||||
byte[] buffer = null;
|
||||
int length = 0;
|
||||
int numBytes = 0;
|
||||
|
||||
// Check for size of file
|
||||
if (file.length() > Integer.MAX_VALUE) {
|
||||
throw new IllegalStateException("File is too big for this convenience method (" + file.length() + " bytes).");
|
||||
} // end if: file too big for int index
|
||||
buffer = new byte[(int) file.length()];
|
||||
|
||||
// Open a stream
|
||||
bis = new Base64.InputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(file)), Base64.DECODE);
|
||||
|
||||
// Read until done
|
||||
while ((numBytes = bis.read(buffer, length, 4096)) >= 0)
|
||||
length += numBytes;
|
||||
|
||||
// Save in a variable to return
|
||||
decodedData = new byte[length];
|
||||
System.arraycopy(buffer, 0, decodedData, 0, length);
|
||||
|
||||
} // end try
|
||||
catch (java.io.IOException e) {
|
||||
throw new IllegalStateException("Error decoding from file " + filename);
|
||||
} // end catch: IOException
|
||||
finally {
|
||||
try {
|
||||
bis.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} // end finally
|
||||
|
||||
return decodedData;
|
||||
} // end decodeFromFile
|
||||
|
||||
/**
|
||||
* Convenience method for reading a binary file and base64-encoding it.
|
||||
*
|
||||
* @param filename Filename for reading binary data
|
||||
* @return base64-encoded string or null if unsuccessful
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public static String encodeFromFile(String filename) {
|
||||
String encodedData = null;
|
||||
Base64.InputStream bis = null;
|
||||
try {
|
||||
// Set up some useful variables
|
||||
java.io.File file = new java.io.File(filename);
|
||||
byte[] buffer = new byte[(int) (file.length() * 1.4)];
|
||||
int length = 0;
|
||||
int numBytes = 0;
|
||||
|
||||
// Open a stream
|
||||
bis = new Base64.InputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(file)), Base64.ENCODE);
|
||||
|
||||
// Read until done
|
||||
while ((numBytes = bis.read(buffer, length, 4096)) >= 0)
|
||||
length += numBytes;
|
||||
|
||||
// Save in a variable to return
|
||||
encodedData = new String(buffer, 0, length, Base64.PREFERRED_ENCODING);
|
||||
|
||||
} // end try
|
||||
catch (java.io.IOException e) {
|
||||
throw new IllegalStateException("Error encoding from file " + filename);
|
||||
} // end catch: IOException
|
||||
finally {
|
||||
try {
|
||||
bis.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} // end finally
|
||||
|
||||
return encodedData;
|
||||
} // end encodeFromFile
|
||||
|
||||
/* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue