private PushNotificationClient pushNotificationClient() throws IOException { return new PushNotificationClient( "passTypeIdentifier", IOUtils.toString(new Base64InputStream(getClass().getClassLoader().getResourceAsStream("wallet/test.p12"), true)), "test", "true") { @Override public void connect() { } @Override public PushNotificationResponse sendPushNotification(String pushToken) { return new PushNotificationResponse(true, "", false); } @Override public void disconnect() { } }; }
public void _testBinaryReader() throws Exception { // read file and store in occurrence String file = TestFileUtils.getTestInputFile("various", "blob.gif"); Reader ri = new InputStreamReader(new Base64InputStream(new FileInputStream(file), true), "utf-8"); occurrence.setReader(ri, file.length(), DataTypes.TYPE_BINARY); assertTrue("Occurrence datatype is incorrect", Objects.equals(DataTypes.TYPE_BINARY, occurrence.getDataType())); // read and decode occurrence content Reader ro = occurrence.getReader(); assertTrue("Reader value is null", ro != null); InputStream in = new Base64InputStream(new ReaderInputStream(ro, "utf-8"), false); try { OutputStream out = new FileOutputStream("/tmp/blob.gif"); try { IOUtils.copy(in, out); } finally { out.close(); } } finally { in.close(); } }
@Override public void onSubmit() { FileUpload upload = uploadField.getFileUpload(); if (upload != null) { try { Reader input = new InputStreamReader(new Base64InputStream(upload.getInputStream(), true), "utf-8"); FieldInstance fieldInstance = fieldValueModel.getFieldInstanceModel().getFieldInstance(); StringWriter swriter = new StringWriter(); IOUtils.copy(input, swriter); String value = swriter.toString(); fieldInstance.addValue(value, getLifeCycleListener()); fieldValueModel.setExistingValue(value); uploaded = true; } catch (IOException e) { e.printStackTrace(); } } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { CertificationRequest csr = decoder.decode(new Base64InputStream(request.getInputStream())); try { response.setContentType(APPLICATION_PKCS7_MIME); response.addHeader("Content-Transfer-Encoding", "base64"); X509Certificate certificate = est.enroll(csr); Base64OutputStream bOut = new Base64OutputStream(response.getOutputStream()); encoder.encode(bOut, certificate); bOut.flush(); bOut.close(); } catch (IOException e) { response.sendError(500); response.getWriter().write(e.getMessage()); response.getWriter().close(); } // 202 // Retry-After // 400 }
@Override public void transform(InputStream inputStream) throws XMLStreamException { if (getOutputStream() != null) { super.transform(inputStream); } else { super.transform(new Base64InputStream(inputStream, false)); } }
/** * Decode an input stream and write processed data to an output stream * @param in the input stream to be decoded * @param out the output stream to write the decoded data * @throws IOException */ public static void decode(InputStream in, OutputStream out) throws IOException { Base64InputStream base64is = new Base64InputStream(in); copy(base64is, out); }
/** * Encode an input stream and write processed data to an output stream * @param in the input stream to be encoded * @param out the output stream to write the encoded data * @throws IOException */ public static void encode(InputStream in, OutputStream out) throws IOException { Base64InputStream base64is = new Base64InputStream(in, true, DEFAULT_LINE_LENGTH, DEFAULT_LINE_SEPARATOR); copy(base64is, out); }
PKSigningInformation createSigningInformation() { InputStream appleWwdrcaAsStream = getClass().getClassLoader().getResourceAsStream("wallet/AppleWWDRCA.pem"); InputStream base64EncodedPrivateKeyAndCertificatePkcs12AsStream = new ByteArrayInputStream(privateKeyP12Base64.getBytes(StandardCharsets.UTF_8)); Base64InputStream privateKeyAndCertificatePkcs12AsStream = new Base64InputStream(base64EncodedPrivateKeyAndCertificatePkcs12AsStream); try { return new PKSigningInformationUtil().loadSigningInformationFromPKCS12AndIntermediateCertificate(privateKeyAndCertificatePkcs12AsStream, privateKeyPassPhrase, appleWwdrcaAsStream); } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException | NoSuchProviderException | UnrecoverableKeyException e) { throw new RuntimeException("Problem creating pass signing information", e); } }
@Autowired public PushNotificationClient(@Value("${wallet.pass.type.identifier}") String passTypeIdentifier, @Value("${wallet.private.key.p12.base64}") String privateKeyP12Base64, @Value("${wallet.private.key.passphrase}") String privateKeyPassPhrase, @Value("${wallet.push.notifications.enabled:true}") String pushNotificationsEnabled) { this.passTypeIdentifier = passTypeIdentifier; this.pushNotificationsEnabled = !"false".equalsIgnoreCase(pushNotificationsEnabled); ByteArrayInputStream base64EncodedPrivateKeyAndCertificatePkcs12AsStream = new ByteArrayInputStream(privateKeyP12Base64.getBytes(StandardCharsets.UTF_8)); Base64InputStream privateKeyAndCertificatePkcs12AsStream = new Base64InputStream(base64EncodedPrivateKeyAndCertificatePkcs12AsStream); try { this.client = new ApnsClient<>(privateKeyAndCertificatePkcs12AsStream, privateKeyPassPhrase); } catch (SSLException e) { throw new RuntimeException("Problem creating APNs client", e); } }
static void decompressStream(InputStream inputStream, OutputStream outputStream) throws IOException { @SuppressWarnings("resource") InputStream iStream = new GZIPInputStream(new Base64InputStream(new BufferedInputStream(inputStream), false, -1, null)); OutputStream oStream = new BufferedOutputStream(outputStream); byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = iStream.read(buffer)) != -1) { oStream.write(buffer, 0, bytesRead); } oStream.flush(); }
@Override public File convertFromBase64Png(String base64Png) { try { IOUtils.copy(new Base64InputStream(IOUtils.toInputStream(base64Png)), new FileOutputStream(file)); } catch (IOException e) { throw new WebDriverException(e); } return file; }
public DataURIEncodingInputStream(final InputStream in, final MediaType type) { // Only text documents should be URL-encoded. It doesn't matter if the encoding is supported or not because // the URL-encoder works on raw bytes. Everything else must be base-64-encoded. if (type.getType().equals("text")) { this.prepend = ("data:" + type + ",").getBytes(StandardCharsets.US_ASCII); this.encoder = new URLEncodingInputStream(in); } else { this.prepend = ("data:" + type + ";base64,").getBytes(StandardCharsets.US_ASCII); this.encoder = new Base64InputStream(in, true, -1, null); } }
/** * Converts the given base 64 string into a BufferedImage object. * @param string the base 64 string * @return BufferedImage * @throws IOException if an exception occurs reading the image data */ public static final BufferedImage getBase64StringImage(String string) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(string.getBytes()); BufferedInputStream bis = new BufferedInputStream(bais); Base64InputStream b64is = new Base64InputStream(bis); return ImageIO.read(b64is); }
@Override public InputStream getInputStream() throws ResourceStreamNotFoundException { try { return new Base64InputStream(new ReaderInputStream(reader, "utf-8"), false); } catch (UnsupportedEncodingException e) { throw new OntopiaRuntimeException(e); } }
public X509Certificate[] obtainCaCertificates() throws IOException { HttpGet get = new HttpGet(buildUrl(CA_CERTIFICATES_DISTRIBUTION)); HttpResponse response = httpClient.execute(get); int statusCode = response.getStatusLine().getStatusCode(); checkStatusCode(statusCode, 200); checkContentType(response); checkContentTransferEncoding(response); HttpEntity entity = response.getEntity(); X509Certificate[] certs = certDecoder.decode(new Base64InputStream(entity.getContent())); return certs; }
private EnrollmentResponse enroll(CertificationRequest csr, String command) throws IOException { HttpPost post = new HttpPost(buildUrl(command)); post.addHeader("Content-Type", "application/pkcs10"); post.addHeader("Content-Transfer-Encoding", "base64"); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); Base64OutputStream base64Out = new Base64OutputStream(bOut); csrEncoder.encode(base64Out, csr); base64Out.flush(); base64Out.close(); post.setEntity(new ByteArrayEntity(bOut.toByteArray())); HttpResponse response = httpClient.execute(post); int statusCode = response.getStatusLine().getStatusCode(); checkStatusCode(statusCode, 200, 202); if (statusCode == 200) { checkContentType(response); checkContentTransferEncoding(response); HttpEntity entity = response.getEntity(); X509Certificate[] certs = certDecoder.decode(new Base64InputStream(entity.getContent())); return new EnrollmentResponse(certs[0]); } else { String retryAfter = response.getFirstHeader("Retry-After").getValue(); return new EnrollmentResponse(RetryAfterParser.parse(retryAfter)); } }
public static void streamBlob(Blob blob, String column, String charset, boolean blobIsCompressed, String blobBase64Direction, Object target, boolean close) throws JdbcException, SQLException, IOException { if (target==null) { throw new JdbcException("cannot stream Blob to null object"); } OutputStream outputStream=StreamUtil.getOutputStream(target); if (outputStream!=null) { InputStream inputStream = JdbcUtil.getBlobInputStream(blob, column, blobIsCompressed); if ("decode".equalsIgnoreCase(blobBase64Direction)){ Base64InputStream base64DecodedStream = new Base64InputStream (inputStream); StreamUtil.copyStream(base64DecodedStream, outputStream, 50000); } else if ("encode".equalsIgnoreCase(blobBase64Direction)){ Base64InputStream base64EncodedStream = new Base64InputStream (inputStream, true); StreamUtil.copyStream(base64EncodedStream, outputStream, 50000); } else { StreamUtil.copyStream(inputStream, outputStream, 50000); } if (close) { outputStream.close(); } return; } Writer writer = StreamUtil.getWriter(target); if (writer !=null) { Reader reader = JdbcUtil.getBlobReader(blob, column, charset, blobIsCompressed); StreamUtil.copyReaderToWriter(reader, writer, 50000, false, false); if (close) { writer.close(); } return; } throw new IOException("cannot stream Blob to ["+target.getClass().getName()+"]"); }
@Override public File getImageFile() throws IOException { if (empty(getImage())) return null; final File temp = temp("."+getFormat()); final Base64InputStream stream = new Base64InputStream(new ByteArrayInputStream(image.getBytes())); FileUtil.toFile(temp, stream); return temp; }
@Override public InputStream doSomething(InputStream inputStream) { return new Base64InputStream(inputStream); }
@Override public Object unmarshal(Exchange exchange, InputStream input) throws Exception { return new Base64InputStream(input, false, lineLength, lineSeparator); }
private void setStreamB64(InputStream B64InputStream) { FileStream=new Base64InputStream(B64InputStream,false); }
@Override public InputStream getInputStream() throws IOException { InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); return new Base64InputStream(stream); }
@Override public InputStream input(InputStream in) throws IOException { return new Base64InputStream(in); }
private InputStream decodeInputStreamWithApache(InputStream origStream) { return new Base64InputStream(origStream); }
/** * Converts the given {@code HttpResponse} into JSON form, with at least * one field, dataUri, containing a Data URI that can be inlined into an HTML page. * Any metadata on the given {@code HttpResponse} is also added as fields. * * @param response Input HttpResponse to convert to JSON. * @return JSON-containing HttpResponse. * @throws IOException If there are problems reading from {@code response}. */ public static HttpResponse convertToJsonResponse(HttpResponse response) throws IOException { // Pull out charset, if present. If not, this operation simply returns contentType. String contentType = response.getHeader("Content-Type"); if (contentType == null) { contentType = ""; } else if (contentType.contains(";")) { contentType = StringUtils.split(contentType, ';')[0].trim(); } // First and most importantly, emit dataUri. // Do so in streaming fashion, to avoid needless buffering. ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("{\n "); pw.write(DATA_URI_KEY); pw.write(":'data:"); pw.write(contentType); pw.write(";base64;charset="); pw.write(response.getEncoding()); pw.write(","); pw.flush(); // Stream out the base64-encoded data. // Ctor args indicate to encode w/o line breaks. Base64InputStream b64input = new Base64InputStream(response.getResponse(), true, 0, null); byte[] buf = new byte[1024]; int read = -1; try { while ((read = b64input.read(buf, 0, 1024)) > 0) { os.write(buf, 0, read); } } finally { IOUtils.closeQuietly(b64input); } // Complete the JSON object. pw.write("',\n "); boolean first = true; for (Map.Entry<String, String> metaEntry : response.getMetadata().entrySet()) { if (DATA_URI_KEY.equals(metaEntry.getKey())) continue; if (!first) { pw.write(",\n "); } first = false; pw.write("'"); pw.write(StringEscapeUtils.escapeJavaScript(metaEntry.getKey()).replace("'", "\'")); pw.write("':'"); pw.write(StringEscapeUtils.escapeJavaScript(metaEntry.getValue()).replace("'", "\'")); pw.write("'"); } pw.write("\n}"); pw.flush(); return new HttpResponseBuilder().setHeader("Content-Type", "application/json").setResponseNoCopy(os.toByteArray()).create(); }
@Test public void testExampleBody() throws Exception { X509Certificate[] certs = decoder.decode(new Base64InputStream(getClass().getResourceAsStream("/cacerts.msg"))); assertEquals("Expected four certificates", 4, certs.length); }
public static String readAsDataURL(InputStream is, String contentType) throws IOException { String base64file = IOUtils.toString(new Base64InputStream(is, true, 0, null)); return "data:" + contentType + ";base64," + base64file; }
/** * <p>Gets a stream which decodes data coming from the given input stream.</p> * * <p>NOTE: Accepted encoding algorithms are: <code>base64</code> or <code>gzip</code>.</p> * * @param encodedStream Input stream on encoded data. * @param encoding Name of the encoding algorithm (<code>base64</code> or <code>gzip</code>). * * @return An input stream which decodes the encoded data read from the given input stream. * * @throws IOException If there is an error while building the input stream. * * @see Base64InputStream * @see GZIPInputStream */ private InputStream getDecodedStream(final InputStream encodedStream, final String encoding) throws IOException { if (encoding == null || encoding.isEmpty()) { return encodedStream; } else if (encoding.equalsIgnoreCase("base64")) { return new Base64InputStream(encodedStream); } else if (encoding.equalsIgnoreCase("gzip")) { return new GZIPInputStream(encodedStream); } else { throw new BinaryInterpreterException("Unknown encoding \"" + encoding + "\" ! It must be either \"base64\" or \"gzip\" !"); } }