Output Streams – Java MemoryStream

Output Streams

When performing HTML conversion operations, results are typically saved to a file. However, certain scenarios require storing results in memory or sending them to remote storage. You can easily do this by implementing the specialized MemoryStreamProvider class. This class represents a callback mechanism required to handle creating and managing output streams, for example, during rendering.

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.

 1public class MemoryStreamProvider implements java.io.Closeable {
 2    // List of InputStream objects created during the document rendering
 3    public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();
 4
 5    @Override
 6    public void close() throws java.io.IOException {
 7        for (java.io.InputStream stream : lStream) {
 8            stream.close();
 9        }
10    }
11}

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
 2MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
 3
 4// Initialize an HTMLDocument instance
 5HTMLDocument document = new HTMLDocument("<span>Hello, World!!</span>", ".");
 6
 7// Convert HTML to JPG using the MemoryStreamProvider
 8Converter.convertHTML(document, new ImageSaveOptions(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.nio.file.Files.copy(memory, new java.io.File("output.jpg").toPath());

Conclusion

The MemoryStreamProvider class is a versatile and important component when handling in-memory rendering operations with Aspose.HTML for Java. By implementing the java.io.Closeable interface, MemoryStreamProvider provides efficient resource management by allowing streams to be explicitly closed after use. Its primary functionality is to serve as a callback mechanism for creating and managing output streams, for example, during rendering processes.

The lStream list, which contains multiple InputStream objects, makes MemoryStreamProvider particularly useful for scenarios requiring multiple output streams, such as rendering multi-page HTML documents to individual images. This class not only simplifies the process of managing rendering output but also provides the flexibility to persist the results in memory before storing them in persistent storage or streaming them to remote systems.

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.