Output Streams – Java MemoryStream

Output Streams

In conversion operations, we normally save the rendering result to the file. However, for some cases, you may need to store the result in the memory or send it to the remote storage. You can easily do this by implementing the specialized MemoryStreamProvider interface and use it as an input parameter to the converter. This interface represents a callback that uses when a new output stream is required.

Note: It may be invoked a few times when the multiple output streams are required. The scenario when this case is possible is rendering HTML to the set of image files.

MemoryStreamProvider class

Aspose.HTML for Java library allows realizing the MemoryStreamProvider class as a custom implementation of the java.io.Closeable interface. It will enable the close() method to be called on instances of this class to release any resources held by the MemoryStreamProvider object. The MemoryStreamProvider class has a public field lStream which is a list java.util.List of java.io.InputStream objects. These objects represent the input streams created during the document rendering process.

The Java code below is an example of the application that uses MemoryStreamProvider to store the rendering result in memory and flush it to the file later:

 1// @java com/aspose/html/documentation/examples/MemoryStreamProvider.java
 2package com.aspose.html.documentation.examples;
 3
 4// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
 5public class MemoryStreamProvider implements java.io.Closeable {
 6
 7    // List of InputStream objects created during document rendering
 8    public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();
 9
10    @Override
11    public void close() throws java.io.IOException {
12        for (java.io.InputStream stream : lStream) {
13            stream.close();
14        }
15    }
16}

MemoryStream to File

The following Java code demonstrates how to use the MemoryStreamProvider class and the Aspose.HTML for Java library to convert HTML to JPG and save the image to a file.

  1. Create a new instance of the MemoryStreamProvider class. It will be used as an output stream for the rendering process.
  2. Initialize an HTML document using the HTMLDocument class. The HTML content is passed as a parameter to the HTMLDocument() constructor.
  3. Call the convertHTML(document, options, provider) method to convert the HTMLDocument object to an image using save options and MemoryStreamProvider.
  4. Use the streamProvider.lStream.get(0) method to retrieve the first InputStream object from the lStream list of the streamProvider object and assign it to the memory variable for further manipulation. This way, you access the memory stream containing the result data.
  5. Use the reset() method to reset the stream position back to the beginning.
 1// @java com/aspose/html/documentation/examples/Advanced_MemoryStreamToFile.java
 2// Create an instance of MemoryStreamProvider
 3MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 4
 5// Initialize an HTMLDocument instance
 6HTMLDocument document = new HTMLDocument("<span>Hello World!!</span>");
 7
 8// Convert HTML to JPG using the MemoryStreamProvider
 9Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider.lStream);
10
11// Get access to the memory stream that contains the result data
12java.io.InputStream memory = streamProvider.lStream.get(0);
13memory.reset();
14
15// Flush the result data to the output file
16java.io.FileOutputStream fs = new java.io.FileOutputStream("output.jpg");
17java.nio.file.Files.copy(memory, new java.io.File("output.jpg").toPath());

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.