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:

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

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// Create an instance of MemoryStreamProvider
 2MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 3
 4// Initialize an HTMLDocument instance
 5com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("<span>Hello World!!</span>");
 6
 7// Convert HTML to JPG using the MemoryStreamProvider
 8com.aspose.html.converters.Converter.convertHTML(document, new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Jpeg), streamProvider.lStream);
 9
10// Get access to the memory stream that contains the result data
11java.io.InputStream memory = streamProvider.lStream.get(0);
12memory.reset();
13
14// Flush the result data to the output file
15java.io.FileOutputStream fs = new java.io.FileOutputStream("output.jpg");
16java.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.