/** * Outputs base64 representation of the specified stream data to an * <code>OutputStream</code>. * * @param in stream data to be encoded * @param out stream where the encoded data should be written to * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, OutputStream out) throws IOException { Writer writer = new OutputStreamWriter(out, CHARSET); encode(in, writer); }
/** * From a byte[] returns a base 64 representation * @param data byte[] * @return String * @throws IOException * @throws IOException */ public static String byteToBase64(byte[] data) throws IOException { StringWriter writer = new StringWriter(); Base64.encode(data, 0, data.length, writer); return writer.toString(); }
/** * Outputs base64 representation of the specified stream data to a * <code>Writer</code>. * * @param in stream data to be encoded * @param writer writer to output the encoded data * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, Writer writer) throws IOException { // encode stream data in chunks; // chunksize must be a multiple of 3 in order // to avoid padding within output byte[] buffer = new byte[9 * 1024]; int read; while ((read = in.read(buffer)) > 0) { encode(buffer, 0, read, writer); } }
/** * Outputs base64 representation of the specified stream data to a * <code>Writer</code>. * * @param in stream data to be encoded * @param writer writer to output the encoded data * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, Writer writer) throws IOException { // encode stream data in chunks; // chunksize must be a multiple of 3 in order // to avoid padding within output byte[] buffer = new byte[9 * 1024]; int read; while ((read = in.read(buffer)) > 0) { encode(buffer, 0, read, writer); } }
/** * Outputs base64 representation of the specified stream data to a * <code>Writer</code>. * * @param in stream data to be encoded * @param writer writer to output the encoded data * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, Writer writer) throws IOException { // encode stream data in chunks; // chunksize must be a multiple of 3 in order // to avoid padding within output byte[] buffer = new byte[9 * 1024]; int read; while ((read = in.read(buffer)) > 0) { encode(buffer, 0, read, writer); } }
/** * Outputs base64 representation of the specified stream data to a * <code>Writer</code>. * * @param in stream data to be encoded * @param writer writer to output the encoded data * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, Writer writer) throws IOException { // encode stream data in chunks; // chunksize must be a multiple of 3 in order // to avoid padding within output byte[] buffer = new byte[9 * 1024]; int read; while ((read = in.read(buffer)) > 0) { encode(buffer, 0, read, writer); } }
/** * Returns the base64 representation of UTF-8 encoded string. * * @since Apache Jackrabbit 2.3 * @param data the string to be encoded * @return base64-encoding of the string */ public static String encode(String data) { try { StringWriter buffer = new StringWriter(); byte[] b = data.getBytes(StandardCharsets.UTF_8); encode(b, 0, b.length, buffer); return buffer.toString(); } catch (IOException e) { // should never happen throw new RuntimeException( "Unable to encode base64 data: " + data, e); } }
/** * Returns the base64 representation of UTF-8 encoded string. * * @since Apache Jackrabbit 2.3 * @param data the string to be encoded * @return base64-encoding of the string */ public static String encode(String data) { try { StringWriter buffer = new StringWriter(); byte[] b = data.getBytes(StandardCharsets.UTF_8); encode(b, 0, b.length, buffer); return buffer.toString(); } catch (IOException e) { // should never happen throw new RuntimeException( "Unable to encode base64 data: " + data, e); } }
/** * Outputs base64 representation of the specified stream data to an * <code>OutputStream</code>. * * @param in stream data to be encoded * @param out stream where the encoded data should be written to * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, OutputStream out) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); try { encode(in, writer); } finally { try { writer.flush(); } catch (IOException ignore) { } } }
/** * Returns the base64 representation of UTF-8 encoded string. * * @since Apache Jackrabbit 2.3 * @param data the string to be encoded * @return base64-encoding of the string */ public static String encode(String data) { try { StringWriter buffer = new StringWriter(); byte[] b = data.getBytes(StandardCharsets.UTF_8); encode(b, 0, b.length, buffer); return buffer.toString(); } catch (IOException e) { // should never happen throw new RuntimeException( "Unable to encode base64 data: " + data, e); } }
/** * Outputs base64 representation of the specified stream data to an * <code>OutputStream</code>. * * @param in stream data to be encoded * @param out stream where the encoded data should be written to * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, OutputStream out) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); try { encode(in, writer); } finally { try { writer.flush(); } catch (IOException ignore) { } } }
/** * Outputs base64 representation of the specified stream data to an * <code>OutputStream</code>. * * @param in stream data to be encoded * @param out stream where the encoded data should be written to * @throws java.io.IOException if an i/o error occurs */ public static void encode(InputStream in, OutputStream out) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); try { encode(in, writer); } finally { try { writer.flush(); } catch (IOException ignore) { } } }
/** * Base64-decodes or -encodes the given string, depending on whether * or not it contains a "{base64}" prefix. If the string gets encoded, * the "{base64}" prefix is added to it. * * @since Apache Jackrabbit 2.3 * @param data string to be decoded or encoded * @return decoded or encoded string */ public static String decodeOrEncode(String data) { if (data.startsWith("{base64}")) { return decode(data.substring("{base64}".length())); } else { return "{base64}" + encode(data); } }
/** * Base64-decodes or -encodes the given string, depending on whether * or not it contains a "{base64}" prefix. If the string gets encoded, * the "{base64}" prefix is added to it. * * @since Apache Jackrabbit 2.3 * @param data string to be decoded or encoded * @return decoded or encoded string */ public static String decodeOrEncode(String data) { if (data.startsWith("{base64}")) { return decode(data.substring("{base64}".length())); } else { return "{base64}" + encode(data); } }
/** * Base64-decodes or -encodes the given string, depending on whether * or not it contains a "{base64}" prefix. If the string gets encoded, * the "{base64}" prefix is added to it. * * @since Apache Jackrabbit 2.3 * @param data string to be decoded or encoded * @return decoded or encoded string */ public static String decodeOrEncode(String data) { if (data.startsWith("{base64}")) { return decode(data.substring("{base64}".length())); } else { return "{base64}" + encode(data); } }
@Override public String serialize(Blob blob) { checkArgument(blob.length() < maxSize, "Cannot serialize Blob of size [%s] " + "which is more than allowed maxSize of [%s]", blob.length(), maxSize); try { try (InputStream is = blob.getNewStream()) { StringWriter writer = new StringWriter(); Base64.encode(is, writer); return writer.toString(); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public String serialize(Blob blob) { checkArgument(blob.length() < maxSize, "Cannot serialize Blob of size [%s] " + "which is more than allowed maxSize of [%s]", blob.length(), maxSize); try { try (InputStream is = blob.getNewStream()) { StringWriter writer = new StringWriter(); Base64.encode(is, writer); return writer.toString(); } } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testCompleteDirectUploadSignatureMustMatch() throws DataRecordUploadException, DataStoreException { DataRecordUpload uploadContext = getDataStore().initiateDataRecordUpload(ONE_MB, 1); // Pull the blob id out and modify it String uploadToken = uploadContext.getUploadToken(); String[] parts = uploadToken.split("#"); String tokenPart = parts[0]; String sigPart = parts[1]; String[] subParts = tokenPart.split("#"); String blobId = subParts[0]; char c = (char)((int)(blobId.charAt(blobId.length()-1))+1); blobId = blobId.substring(0, blobId.length()-1) + c; for (int i = 1; i<subParts.length; i++) { blobId += "#" + subParts[i]; } String newToken = Base64.encode(blobId) + "#" + sigPart; try { getDataStore().completeDataRecordUpload(newToken); fail(); } catch (IllegalArgumentException e) { } }