@Override public CxZipResult invoke(final File file, final VirtualChannel channel) throws IOException, InterruptedException { final File tempFile = File.createTempFile("base64ZippedSource", ".bin"); FilePath remoteTempFile = new FilePath(tempFile); final OutputStream fileOutputStream = new FileOutputStream(tempFile); final Base64OutputStream base64FileOutputStream = new Base64OutputStream(fileOutputStream, true, 0, null); ZippingDetails zippingDetails; try { zippingDetails = new Zipper().zip(file, combinedFilterPattern, base64FileOutputStream, CxConfig.maxZipSize()); } catch (Exception e) { deleteTempFile(remoteTempFile); throw e; } finally { fileOutputStream.close(); } return new CxZipResult(remoteTempFile, zippingDetails); }
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 }
public void encodeFileToBase64Stream(Path file, OutputStream base64OutputStream) throws IOException { InputStream inputStream = Files.newInputStream(file); OutputStream out = new Base64OutputStream(base64OutputStream, true); IOUtils.copy(inputStream, out); inputStream.close(); out.close(); }
@Override public void setOutputStream(OutputStream outputStream) throws XMLSecurityException { super.setOutputStream(new Base64OutputStream( new FilterOutputStream(outputStream) { @Override public void close() throws IOException { //do not close the parent output stream! super.flush(); } }, false) ); }
private void marshalStreaming(Exchange exchange, Object graph, OutputStream stream) throws Exception { InputStream decoded = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph); Base64OutputStream base64Output = new Base64OutputStream(stream, true, lineLength, lineSeparator); try { IOHelper.copy(decoded, base64Output); } finally { IOHelper.close(decoded, base64Output); } }
static void compressStream(InputStream inputStream, OutputStream outputStream) throws IOException { InputStream iStream = new BufferedInputStream(inputStream); GZIPOutputStream oStream = new GZIPOutputStream(new Base64OutputStream(new BufferedOutputStream(outputStream), true, -1, null), 2048); byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = iStream.read(buffer)) != -1) { oStream.write(buffer, 0, bytesRead); } oStream.close(); // this is necessary for the gzip and base64 streams }
/** * Converts the given image into a base64 encoded string. * @param image the image * @return String * @throws IOException if an exception occurs during write */ public static final String getBase64ImageString(RenderedImage image) throws IOException { ByteArrayOutputStream bo = new ByteArrayOutputStream(); Base64OutputStream b64o = new Base64OutputStream(bo); ImageIO.write(image, "png", b64o); return new String(bo.toByteArray()); }
@Override public void convertString(UTF8StringPointable stringp, DataOutput dOut) throws SystemException, IOException { baaos.reset(); Base64OutputStream b64os = new Base64OutputStream(baaos, false); b64os.write(stringp.getByteArray(), stringp.getCharStartOffset(), stringp.getUTF8Length()); dOut.write(ValueTag.XS_BASE64_BINARY_TAG); dOut.write((byte) ((baaos.size() >>> 8) & 0xFF)); dOut.write((byte) ((baaos.size() >>> 0) & 0xFF)); dOut.write(baaos.getByteArray(), 0, baaos.size()); b64os.close(); }
@Override public void convertBase64Binary(XSBinaryPointable binaryp, DataOutput dOut) throws SystemException, IOException { // Read binary Base64OutputStream b64os = new Base64OutputStream(baaos, true); b64os.write(binaryp.getByteArray(), binaryp.getStartOffset() + 2, binaryp.getLength() - 2); // Write string startString(); sb.appendUtf8Bytes(baaos.getByteArray(), 0, baaos.size()); sendStringDataOutput(dOut); }
public String imageToString(String path) throws Exception { BufferedImage img = ImageIO.read(new File(path)); ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = new Base64OutputStream(os); ImageIO.write(img, "png", b64); String result = os.toString("UTF-8"); HttpURLConnection conn; URL url = new URL("https://api.imgur.com/3/image"); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization", "Client-ID " + "4d9fc949d3d87e9"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); StringBuilder stb = new StringBuilder(); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(result); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { stb.append(line).append("\n"); } wr.close(); rd.close(); return stb.toString(); }
@Benchmark public byte[] write1KRecordsBase64Only(EncodingBenchmarkState state) throws IOException { ByteArrayOutputStream sink = new ByteArrayOutputStream(); OutputStream os = new Base64OutputStream(sink); os.write(state.OneKBytes); os.close(); return sink.toByteArray(); }
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)); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(APPLICATION_PKCS7_MIME); response.addHeader("Content-Transfer-Encoding", "base64"); Base64OutputStream bOut = new Base64OutputStream(response.getOutputStream()); encoder.encode(bOut, est.getCaCertificates()); bOut.flush(); bOut.close(); }
public static String encodeBase64ZippedString( String in ) throws IOException { Charset charset = Charset.forName( Const.XML_ENCODING ); ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 ); try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos ); GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) { gzos.write( in.getBytes( charset ) ); } return baos.toString(); }
/** * Base64 encodes then zips a byte array into a compressed string * * @param src the source byte array * @return a compressed, base64 encoded string * @throws IOException */ public static String encodeBase64Zipped( byte[] src ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 ); try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos ); GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) { gzos.write( src ); } return baos.toString(); }
/** * Generate an encoded base64 String with qrcode image * * @param credentials * @param width * @param height * @return String * @throws Exception */ @Override public ServiceResponse<String> generateQrCodeAccess(DeviceSecurityCredentials credentials, int width, int height, Locale locale) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Base64OutputStream encoded = new Base64OutputStream(baos); StringBuilder content = new StringBuilder(); content.append("{\"user\":\"").append(credentials.getDevice().getUsername()); content.append("\",\"pass\":\"").append(credentials.getPassword()); DeviceDataURLs deviceDataURLs = new DeviceDataURLs(credentials.getDevice(), locale); content.append("\",\"host\":\"").append(deviceDataURLs.getHttpHostName()); content.append("\",\"ctx\":\"").append(deviceDataURLs.getContext()); content.append("\",\"host-mqtt\":\"").append(deviceDataURLs.getMqttHostName()); content.append("\",\"http\":\"").append(deviceDataURLs.getHttpPort()); content.append("\",\"https\":\"").append(deviceDataURLs.getHttpsPort()); content.append("\",\"mqtt\":\"").append(deviceDataURLs.getMqttPort()); content.append("\",\"mqtt-tls\":\"").append(deviceDataURLs.getMqttTlsPort()); content.append("\",\"pub\":\"pub/").append(credentials.getDevice().getUsername()); content.append("\",\"sub\":\"sub/").append(credentials.getDevice().getUsername()).append("\"}"); Map<EncodeHintType, Serializable> map = new HashMap<>(); for (AbstractMap.SimpleEntry<EncodeHintType, ? extends Serializable> item : Arrays.<AbstractMap.SimpleEntry<EncodeHintType, ? extends Serializable>>asList( new AbstractMap.SimpleEntry<>(EncodeHintType.MARGIN, 0), new AbstractMap.SimpleEntry<>(EncodeHintType.CHARACTER_SET, "UTF-8") )) { if (map.put(item.getKey(), item.getValue()) != null) { throw new IllegalStateException("Duplicate key"); } } BitMatrix bitMatrix = new QRCodeWriter().encode( content.toString(), BarcodeFormat.QR_CODE, width, height, Collections.unmodifiableMap(map)); MatrixToImageWriter.writeToStream(bitMatrix, "png", encoded); String result = "data:image/png;base64," + new String(baos.toByteArray(), 0, baos.size(), "UTF-8"); return ServiceResponseBuilder.<String>ok().withResult(result).build(); } catch (Exception e) { return ServiceResponseBuilder.<String>error() .withMessage(Messages.DEVICE_QRCODE_ERROR.getCode()) .build(); } }
private static void decodeToFile(final String dataToDecode, final String filename) throws java.io.IOException { try (final Base64OutputStream os = new Base64OutputStream(new FileOutputStream(filename), false)) { os.write(dataToDecode.getBytes(StandardCharsets.UTF_8)); } }
private static OutputStream compressAndEncodeStream(OutputStream os) { return new DeflaterOutputStream(new Base64OutputStream(os)); }
@Override public OutputStream output(OutputStream out) throws IOException { return new Base64OutputStream(out); }
private OutputStream encodeOutputStreamWithApache(OutputStream origStream) { return new Base64OutputStream(origStream, true, 0, null); }