1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-10-31 19:22:31 +00:00

Extract ImageIO.read(byte[]) method

This commit is contained in:
matthew16550 2021-09-11 14:21:48 +10:00
parent 5e20da92f2
commit 985916664f
10 changed files with 14 additions and 32 deletions

View File

@ -41,7 +41,6 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -88,12 +87,9 @@ public class ClipboardLoop {
Log.info("No image generated"); Log.info("No image generated");
} else { } else {
Log.info("Image ok " + desc.getDescription()); Log.info("Image ok " + desc.getDescription());
final byte[] data = baos.toByteArray();
baos.close(); baos.close();
final ByteArrayInputStream bais = new ByteArrayInputStream(data); final BufferedImage image = ImageIO.read(baos.toByteArray());
final BufferedImage image = ImageIO.read(bais);
setClipboardImage(image); setClipboardImage(image);
bais.close();
Log.info("Image copied in clipboard"); Log.info("Image copied in clipboard");
} }
} }

View File

@ -37,7 +37,6 @@ package net.sourceforge.plantuml;
import java.awt.geom.Dimension2D; import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -187,10 +186,7 @@ public class EmbeddedDiagram implements CharSequence {
final ByteArrayOutputStream os = new ByteArrayOutputStream(); final ByteArrayOutputStream os = new ByteArrayOutputStream();
system.exportDiagram(os, 0, new FileFormatOption(FileFormat.PNG)); system.exportDiagram(os, 0, new FileFormatOption(FileFormat.PNG));
os.close(); os.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); return ImageIO.read(os.toByteArray());
final BufferedImage im = ImageIO.read(is);
is.close();
return im;
} }
public HorizontalAlignment getHorizontalAlignment() { public HorizontalAlignment getHorizontalAlignment() {

View File

@ -42,7 +42,6 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D; import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -270,7 +269,7 @@ public abstract class UmlDiagram extends TitledDiagram implements Diagram, Annot
// exportDiagramTOxxBEREMOVED(baos, null, 0, new // exportDiagramTOxxBEREMOVED(baos, null, 0, new
// FileFormatOption(FileFormat.PNG, at)); // FileFormatOption(FileFormat.PNG, at));
baos.close(); baos.close();
final BufferedImage im = ImageIO.read(new ByteArrayInputStream(baos.toByteArray())); final BufferedImage im = ImageIO.read(baos.toByteArray());
m.addImage(im); m.addImage(im);
} }
m.finishAVI(); m.finishAVI();

View File

@ -38,7 +38,6 @@ package net.sourceforge.plantuml.creole.atom;
import java.awt.Color; import java.awt.Color;
import java.awt.geom.Dimension2D; import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.plantuml.Dimension2DDouble; import net.sourceforge.plantuml.Dimension2DDouble;
@ -157,7 +156,7 @@ public class AtomImg extends AbstractAtom implements Atom {
private static Atom buildRasterFromData(String source, final FontConfiguration fc, final byte[] data, double scale, private static Atom buildRasterFromData(String source, final FontConfiguration fc, final byte[] data, double scale,
Url url) throws IOException { Url url) throws IOException {
final BufferedImage read = ImageIO.read(new ByteArrayInputStream(data)); final BufferedImage read = ImageIO.read(data);
if (read == null) { if (read == null) {
return AtomTextUtils.createLegacy("(Cannot decode: " + source + ")", fc); return AtomTextUtils.createLegacy("(Cannot decode: " + source + ")", fc);
} }

View File

@ -35,9 +35,7 @@
*/ */
package net.sourceforge.plantuml.eggs; package net.sourceforge.plantuml.eggs;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import net.sourceforge.plantuml.PlainStringsDiagram; import net.sourceforge.plantuml.PlainStringsDiagram;
import net.sourceforge.plantuml.core.DiagramDescription; import net.sourceforge.plantuml.core.DiagramDescription;
@ -60,10 +58,7 @@ public class PSystemRIP extends PlainStringsDiagram {
strings.add(" <b>Requiescat In Pace"); strings.add(" <b>Requiescat In Pace");
strings.add(" "); strings.add(" ");
final InputStream is = new ByteArrayInputStream(imm); image = ImageIO.read(imm);
image = ImageIO.read(is);
is.close();
imagePosition = GraphicPosition.BOTTOM; imagePosition = GraphicPosition.BOTTOM;
} }

View File

@ -40,7 +40,6 @@ import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainPngBuilder;
import java.awt.Color; import java.awt.Color;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -119,7 +118,7 @@ public class ScientificEquationSafe {
} }
try { try {
final byte[] bytes = plainPngBuilder(getRollback()).writeByteArray(); final byte[] bytes = plainPngBuilder(getRollback()).writeByteArray();
return new PixelImage(ImageIO.read(new ByteArrayInputStream(bytes)), AffineTransformType.TYPE_BILINEAR); return new PixelImage(ImageIO.read(bytes), AffineTransformType.TYPE_BILINEAR);
} catch (IOException e1) { } catch (IOException e1) {
return null; return null;
} }

View File

@ -37,6 +37,7 @@ package net.sourceforge.plantuml.security;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -77,6 +78,10 @@ public class ImageIO {
return javax.imageio.ImageIO.read(is); return javax.imageio.ImageIO.read(is);
} }
public static BufferedImage read(byte[] bytes) throws IOException {
return javax.imageio.ImageIO.read(new ByteArrayInputStream(bytes));
}
public static ImageInputStream createImageInputStream(java.io.File file) throws IOException { public static ImageInputStream createImageInputStream(java.io.File file) throws IOException {
return javax.imageio.ImageIO.createImageInputStream(file); return javax.imageio.ImageIO.createImageInputStream(file);
} }

View File

@ -52,7 +52,6 @@ import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -336,7 +335,7 @@ class ImageWindow extends JFrame {
final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList(msg), false); final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList(msg), false);
try { try {
final byte[] bytes = plainPngBuilder(error).writeByteArray(); final byte[] bytes = plainPngBuilder(error).writeByteArray();
image = ImageIO.read(new ByteArrayInputStream(bytes)); image = ImageIO.read(bytes);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -43,7 +43,6 @@ import java.awt.Shape;
import java.awt.font.TextLayout; import java.awt.font.TextLayout;
import java.awt.geom.PathIterator; import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -184,7 +183,7 @@ public class FontChecker {
} }
}; };
final byte[] bytes = plainPngBuilder(drawable).writeByteArray(); final byte[] bytes = plainPngBuilder(drawable).writeByteArray();
return ImageIO.read(new ByteArrayInputStream(bytes)); return ImageIO.read(bytes);
} }
// public BufferedImage getBufferedImageOld(char c) throws IOException { // public BufferedImage getBufferedImageOld(char c) throws IOException {

View File

@ -43,7 +43,6 @@ import java.awt.Image;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D; import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -401,11 +400,7 @@ public class ImageBuilder {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeImageInternal(new FileFormatOption(FileFormat.PNG), baos, Animation.singleton(affineTransform)); writeImageInternal(new FileFormatOption(FileFormat.PNG), baos, Animation.singleton(affineTransform));
baos.close(); baos.close();
return ImageIO.read(baos.toByteArray());
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final Image im = ImageIO.read(bais);
bais.close();
return im;
} }
private UGraphic2 createUGraphic(FileFormatOption option, final Dimension2D dim, Animation animationArg, double dx, private UGraphic2 createUGraphic(FileFormatOption option, final Dimension2D dim, Animation animationArg, double dx,