KEYCLOAK-5859
This commit is contained in:
1 parent
f923211e9f
commit
bc28223a77
2 files changed
-395
No files matched your search
@@ -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 ******** */
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user