Is there a way to load a (PlantUML-) generated image, without saving to the file system?

Viewed 534

I'm working with the Plant UML API to generate UML diagrams of tests in my Java project. The API generates an image and saves it to a PNG or SVG file in the file system.

I'm wondering if there's a way to just load the image directly in the console without saving it to the file system (or loading a file already in the file system).

For reference, I'm using the documentation PUML provided here and I'm calling something like:

import net.sourceforge.plantuml.SourceStringReader;
OutputStream png = ...;
String source = "@startuml\n";
source += "Bob -> Alice : hello\n";
source += "@enduml\n";

SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.outputImage(png).getDescription();
// Return a null string if no generation
1 Answers

I guess this should do the job.

public byte[] createUmlImage(String source){
    try {
        SourceStringReader reader = new SourceStringReader(source);
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        reader.outputImage(os);
        return os.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I would also make another suggestion. Make use of StringBuilder's append() for string concatenation instead of the + operator.

Related