Create and return a ByteArrayOutputStream instead of a byte[].
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Manuel Friedli 2023-04-08 20:35:06 +02:00
parent a076681cba
commit 5b8ea13838
3 changed files with 23 additions and 17 deletions

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.Position;
import ch.fritteli.labyrinth.generator.model.Tile; import ch.fritteli.labyrinth.generator.model.Tile;
import io.vavr.control.Option; 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.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Value; import lombok.Value;
@ -16,18 +20,15 @@ import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
class Generator { class Generator {
@NonNull @NonNull
private final Labyrinth labyrinth; private final Labyrinth labyrinth;
@NonNull @NonNull
public byte[] generate() { public ByteArrayOutputStream generate() {
final float pageWidth = this.labyrinth.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN; final float pageWidth = this.labyrinth.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageHeight = this.labyrinth.getHeight() * 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)); final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
pdDocument.addPage(puzzlePage); pdDocument.addPage(puzzlePage);
pdDocument.addPage(solutionPage); 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(puzzlePageContentStream);
setUpPageContentStream(solutionPageContentStream); setUpPageContentStream(solutionPageContentStream);
this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream); this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream);
@ -55,7 +57,7 @@ class Generator {
} catch (IOException e) { } catch (IOException e) {
log.error("Error while writing PDF data", e); log.error("Error while writing PDF data", e);
} }
return output.toByteArray(); return output;
} }
private void setUpPageContentStream(@NonNull final PDPageContentStream pageContentStream) throws IOException { private void setUpPageContentStream(@NonNull final PDPageContentStream pageContentStream) throws IOException {
@ -241,6 +243,7 @@ class Generator {
@Value @Value
private class Coordinate { private class Coordinate {
float x; float x;
float y; float y;
@ -268,6 +271,7 @@ class Generator {
@Value @Value
private class SolutionCoordinate { private class SolutionCoordinate {
float x; float x;
float y; 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.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer; import ch.fritteli.labyrinth.generator.renderer.Renderer;
import java.io.ByteArrayOutputStream;
import lombok.NonNull; import lombok.NonNull;
public class PDFRenderer implements Renderer<byte[]> { public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
static final float MARGIN = 10; static final float MARGIN = 10;
static final float SCALE = 10; static final float SCALE = 10;
@ -18,7 +19,7 @@ public class PDFRenderer implements Renderer<byte[]> {
@NonNull @NonNull
@Override @Override
public byte[] render(@NonNull final Labyrinth labyrinth) { public ByteArrayOutputStream render(@NonNull final Labyrinth labyrinth) {
final Generator generator = new Generator(labyrinth); final Generator generator = new Generator(labyrinth);
return generator.generate(); 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 ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer;
import io.vavr.control.Option; import io.vavr.control.Option;
import io.vavr.control.Try; import io.vavr.control.Try;
import lombok.NonNull; import java.io.ByteArrayOutputStream;
import lombok.extern.slf4j.Slf4j;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class PDFFileRenderer implements Renderer<Path> { public class PDFFileRenderer implements Renderer<Path> {
@NonNull @NonNull
private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance(); private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance();
@NonNull @NonNull
@ -55,13 +56,13 @@ public class PDFFileRenderer implements Renderer<Path> {
throw new IllegalArgumentException("Cannot write to target file.", e); throw new IllegalArgumentException("Cannot write to target file.", e);
} }
} }
final byte[] bytes = PDF_RENDERER.render(labyrinth); final ByteArrayOutputStream bytes = PDF_RENDERER.render(labyrinth);
final Path targetFile = this.targetFile.get(); final Path outputFile = this.targetFile.get();
try { try {
Files.write(targetFile, bytes); bytes.writeTo(Files.newOutputStream(outputFile));
} catch (IOException e) { } 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;
} }
} }