具体的には
import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class GnuplotUser5 { private static final String GNUPLOT = "/usr/bin/gnuplot"; public static void main(String[] args) throws IOException, InterruptedException { Process p = new ProcessBuilder(GNUPLOT, "-").start(); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(p.getOutputStream()))); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); out.println("set terminal png"); out.println("plot sin(x)"); out.println("set output"); out.flush(); BufferedImage image = ImageIO.read(in); System.err.println(image); out.close(); in.close(); p.waitFor(); } }
$ java GnuplotUser5 BufferedImage@18ca663: type = 13 IndexColorModel: #pixelBits = 8 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@ffc3eb transparency = 1 transInde x = -1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 640 height = 480 #numDataElements 1 dataOff[0] = 0
gnuplotからの入力ストリームin
を閉じなくてもimage
が正しく返される。
以前に示したものと同様のout.flush()
が無い場合はImageIO#read
から戻ってこない。
単にフラッシュするのを忘れただけだったのだ。