/** * Write the given temporary OutputStream to the HTTP response. * @param response current HTTP response * @param baos the temporary OutputStream to write * @throws IOException if writing/flushing failed */ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { // Write content type and also length (determined via byte array). response.setContentType(getContentType()); response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); }
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Object toBeMarshalled = locateToBeMarshalled(model); if (toBeMarshalled == null) { throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model); } Assert.state(this.marshaller != null, "No Marshaller set"); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos)); setResponseContentType(request, response); response.setContentLength(baos.size()); baos.writeTo(response.getOutputStream()); }
try { FileOutputStream fos = new FileOutputStream (new File("myFile")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Put data in your baos baos.writeTo(fos); } catch(IOException ioe) { // Handle exception here ioe.printStackTrace(); } finally { fos.close(); }
private <T> void serialize(final T value, final Serializer<T> serializer, final DataOutputStream dos) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(value, baos); dos.writeInt(baos.size()); baos.writeTo(dos); }
public static void saveExistingErrors(final File markFile, final AtomicBuffer errorBuffer, final PrintStream logger) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final int observations = saveErrorLog(new PrintStream(baos, false, "UTF-8"), errorBuffer); if (observations > 0) { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSZ"); final String errorLogFilename = markFile.getParent() + '-' + dateFormat.format(new Date()) + "-error.log"; if (null != logger) { logger.println("WARNING: Existing errors saved to: " + errorLogFilename); } try (FileOutputStream out = new FileOutputStream(errorLogFilename)) { baos.writeTo(out); } } } catch (final Exception ex) { LangUtil.rethrowUnchecked(ex); } }
private <T> void serialize(final Set<T> values, final Serializer<T> serializer, final DataOutputStream dos) throws IOException { // Write the number of elements to follow, then each element and its size dos.writeInt(values.size()); for(T value : values) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(value, baos); dos.writeInt(baos.size()); baos.writeTo(dos); } }
private static void reportExistingErrors(final Context ctx, final MappedByteBuffer cncByteBuffer) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final int observations = ctx.saveErrorLog(new PrintStream(baos, false, "UTF-8"), cncByteBuffer); if (observations > 0) { final StringBuilder builder = new StringBuilder(ctx.aeronDirectoryName()); removeTrailingSlashes(builder); final SimpleDateFormat dateFormat = new SimpleDateFormat("-yyyy-MM-dd-HH-mm-ss-SSSZ"); builder.append(dateFormat.format(new Date())).append("-error.log"); final String errorLogFilename = builder.toString(); System.err.println("WARNING: Existing errors saved to: " + errorLogFilename); try (FileOutputStream out = new FileOutputStream(errorLogFilename)) { baos.writeTo(out); } } } catch (final Exception ex) { LangUtil.rethrowUnchecked(ex); } }
@Override public void writeHeader(final long firstEventId, final DataOutputStream out) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); eventSchema.writeTo(baos); out.writeInt(baos.size()); baos.writeTo(out); }
public void update(final Collection<S> records, final long transactionId, final Map<Object, S> recordMap, final boolean forceSync) throws IOException { try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(256); final DataOutputStream out = new DataOutputStream(baos)) { out.writeLong(transactionId); final int numEditsToSerialize = records.size(); int editsSerialized = 0; for (final S record : records) { final Object recordId = serde.getRecordIdentifier(record); final S previousVersion = recordMap.get(recordId); serde.serializeEdit(previousVersion, record, out); if (++editsSerialized < numEditsToSerialize) { out.write(TRANSACTION_CONTINUE); } else { out.write(TRANSACTION_COMMIT); } } out.flush(); if (this.closed) { throw new IllegalStateException("Partition is closed"); } baos.writeTo(dataOut); dataOut.flush(); if (forceSync) { synchronized (fileOut) { fileOut.getFD().sync(); } } } }
@Override protected void writeRecord(final ProvenanceEventRecord event, final long eventId, final DataOutputStream out) throws IOException { try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(256)) { final Record eventRecord = createRecord(event, eventId); recordWriter.writeRecord(eventRecord, baos); out.writeInt(baos.size()); baos.writeTo(out); } } }
/** * Write the trailer to a data stream. We support writing version 1 for * testing and for determining version 1 trailer size. It is also easy to see * what fields changed in version 2. * * @param outputStream * @throws IOException */ void serialize(DataOutputStream outputStream) throws IOException { HFile.checkFormatVersion(majorVersion); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream baosDos = new DataOutputStream(baos); BlockType.TRAILER.write(baosDos); serializeAsPB(baosDos); // The last 4 bytes of the file encode the major and minor version universally baosDos.writeInt(materializeVersion(majorVersion, minorVersion)); baos.writeTo(outputStream); }
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Object toBeMarshalled = locateToBeMarshalled(model); if (toBeMarshalled == null) { throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model); } Assert.state(this.marshaller != null, "No Marshaller set"); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos)); setResponseContentType(request, response); response.setContentLength(baos.size()); baos.writeTo(response.getOutputStream()); }
final String className = clazz.getClassName().replace('.','/'); jos.putNextEntry(new JarEntry(className+".class")); final ByteArrayOutputStream out = new ByteArrayOutputStream(2048); out.writeTo(jos);
/** * Write the given temporary OutputStream to the HTTP response. * @param response current HTTP response * @param baos the temporary OutputStream to write * @throws IOException if writing/flushing failed */ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { // Write content type and also length (determined via byte array). response.setContentType(getContentType()); response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); }
public static byte[] encodeOid(String oid) { requireNonNull(oid, "oid is null"); List<Integer> parts = Splitter.on('.').splitToList(oid).stream() .map(Integer::parseInt) .collect(toImmutableList()); checkArgument(parts.size() >= 2, "at least 2 parts are required"); try { ByteArrayOutputStream body = new ByteArrayOutputStream(); body.write(parts.get(0) * 40 + parts.get(1)); for (Integer part : parts.subList(2, parts.size())) { writeOidPart(body, part); } byte[] length = encodeLength(body.size()); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(OBJECT_IDENTIFIER_TAG); out.write(length); body.writeTo(out); return out.toByteArray(); } catch (IOException e) { // this won't happen with byte array output streams throw new UncheckedIOException(e); } }
ByteArrayOutputStream baos = new ByteArrayOutputStream(); AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder(); baos.writeTo(fos); baos.flush(); fos.flush();