Create and return a ByteArrayOutputStream instead of a byte[]. #3

Merged
manuel merged 1 commit from feature/pdf-bytearrayoutputstream into master 2023-04-08 22:00:14 +02:00
3 changed files with 23 additions and 17 deletions
Showing only changes of commit 5b8ea13838 - Show all commits

View file

@ -5,6 +5,10 @@ import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import io.vavr.control.Option;
import java.awt.BasicStroke;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
@ -16,18 +20,15 @@ import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@RequiredArgsConstructor
@Slf4j
class Generator {
@NonNull
private final Labyrinth labyrinth;
@NonNull
public byte[] generate() {
public ByteArrayOutputStream generate() {
final float pageWidth = this.labyrinth.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageHeight = this.labyrinth.getHeight() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
@ -39,7 +40,8 @@ class Generator {
final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
pdDocument.addPage(puzzlePage);
pdDocument.addPage(solutionPage);
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage);
final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
setUpPageContentStream(puzzlePageContentStream);
setUpPageContentStream(solutionPageContentStream);
this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream);
@ -55,7 +57,7 @@ class Generator {
} catch (IOException e) {
log.error("Error while writing PDF data", e);
}
return output.toByteArray();
return output;
}
private void setUpPageContentStream(@NonNull final PDPageContentStream pageContentStream) throws IOException {
@ -241,6 +243,7 @@ class Generator {
@Value
private class Coordinate {
float x;
float y;
@ -268,6 +271,7 @@ class Generator {
@Value
private class SolutionCoordinate {
float x;
float y;

View file

@ -2,9 +2,10 @@ package ch.fritteli.labyrinth.generator.renderer.pdf;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import java.io.ByteArrayOutputStream;
import lombok.NonNull;
public class PDFRenderer implements Renderer<byte[]> {
public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
static final float MARGIN = 10;
static final float SCALE = 10;
@ -18,7 +19,7 @@ public class PDFRenderer implements Renderer<byte[]> {
@NonNull
@Override
public byte[] render(@NonNull final Labyrinth labyrinth) {
public ByteArrayOutputStream render(@NonNull final Labyrinth labyrinth) {
final Generator generator = new Generator(labyrinth);
return generator.generate();
}

View file

@ -5,17 +5,18 @@ import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer;
import io.vavr.control.Option;
import io.vavr.control.Try;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PDFFileRenderer implements Renderer<Path> {
@NonNull
private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance();
@NonNull
@ -55,13 +56,13 @@ public class PDFFileRenderer implements Renderer<Path> {
throw new IllegalArgumentException("Cannot write to target file.", e);
}
}
final byte[] bytes = PDF_RENDERER.render(labyrinth);
final Path targetFile = this.targetFile.get();
final ByteArrayOutputStream bytes = PDF_RENDERER.render(labyrinth);
final Path outputFile = this.targetFile.get();
try {
Files.write(targetFile, bytes);
bytes.writeTo(Files.newOutputStream(outputFile));
} catch (IOException e) {
log.error("Failed writing to file " + targetFile.normalize(), e);
log.error("Failed writing to file " + outputFile.normalize(), e);
}
return targetFile;
return outputFile;
}
}