private Icon makeIcon(final String gifFile) throws IOException { /* Copy resource into a byte array. This is * necessary because several browsers consider * Class.getResource a security risk because it * can be used to load additional classes. * Class.getResourceAsStream just returns raw * bytes, which we can convert to an image. */ InputStream resource = MyImageView.class.getResourceAsStream(gifFile); if (resource == null) { System.err.println(MyImageView.class.getName() + "/" + gifFile + " not found!!."); return null; } BufferedInputStream in = new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int n; while ((n = in.read(buffer)) > 0) { out.write(buffer, 0, n); } in.close(); out.flush(); buffer = out.toByteArray(); if (buffer.length == 0) { System.err.println("warning: " + gifFile + " is zero-length"); return null; } return new ImageIcon(buffer); }
/** * Loads the bytes in a file. * * @param name the file name * @return an array with the bytes in the file */ public byte[] loadBytes(String name) throws IOException { FileInputStream in = null; in = new FileInputStream(name); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) buffer.write(ch); return buffer.toByteArray(); } finally { in.close(); } }
public String getData() { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { getDrawing().getOutputFormats().get(0).write(out, getDrawing()); return out.toString("UTF8"); } catch (IOException e) { SVGTextFigure tf = new SVGTextFigure(); tf.setText(e.getMessage()); tf.setBounds(new Point2D.Double(10, 10), new Point2D.Double(100, 100)); getDrawing().add(tf); e.printStackTrace(); return ""; } }
public void run() { String objRouter = applet.getParameter("name"); // No Internationalisation try { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream outp = new DataOutputStream(byteStream); outp.writeInt(GenericConstants.ROUTER_PROPERTIES); outp.writeUTF(objRouter); outp.flush(); byte[] bytes = byteStream.toByteArray(); outp.close(); byteStream.reset(); byteStream.close(); byte[] data = GenericSession.getInstance().syncSend(bytes); if (data != null) { DataInputStream inp = new DataInputStream(new ByteArrayInputStream(data)); int reqId = inp.readInt(); if (reqId == GenericConstants.ROUTER_PROPERTIES) { int length = inp.readInt(); byte serverData[] = new byte[length]; inp.readFully(serverData); routerobject = NmsClientUtil.deSerializeVector(serverData); } init(); refresh(); super.setVisible(true); } /*init(); refresh(); super.setVisible(true);*/ else close(); } catch (Exception e) { // NmsClientUtil.err(NmsClientUtil.getFrame(app),"IO Error sending request to server. // "+e);//No Internationalisation } }
private void compareDataset(CompareDialog.Data data) { if (data.name == null) return; NetcdfFile compareFile = null; try { compareFile = NetcdfDataset.openFile(data.name, null); Formatter f = new Formatter(); CompareNetcdf2 cn = new CompareNetcdf2(f, data.showCompare, data.showDetails, data.readData); if (data.howMuch == CompareDialog.HowMuch.All) cn.compare(ds, compareFile); else { NestedTable nested = nestedTableList.get(0); Variable org = getCurrentVariable(nested.table); if (org == null) return; Variable ov = compareFile.findVariable(org.getFullNameEscaped()); if (ov != null) cn.compareVariable(org, ov); } infoTA.setText(f.toString()); infoTA.gotoTop(); infoWindow.setTitle("Compare"); infoWindow.show(); } catch (Throwable ioe) { ByteArrayOutputStream bos = new ByteArrayOutputStream(10000); ioe.printStackTrace(new PrintStream(bos)); infoTA.setText(bos.toString()); infoTA.gotoTop(); infoWindow.show(); } finally { if (compareFile != null) try { compareFile.close(); } catch (Exception eek) { } } }
public Image getImage(String sImage) { Image imReturn = null; try { if (jar == null) { imReturn = this.toolkit.createImage(this.getClass().getClassLoader().getResource(sImage)); } else { // BufferedInputStream bis = new BufferedInputStream(jar.getInputStream(jar.getEntry(sImage))); ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096); int b; while ((b = bis.read()) != -1) { buffer.write(b); } byte[] imageBuffer = buffer.toByteArray(); imReturn = this.toolkit.createImage(imageBuffer); bis.close(); buffer.close(); } } catch (IOException ex) { } return imReturn; }
/** * Gets an image resource. * * @param strResourceFilename Name of the image file. * @param srcClass Class from which the location of the resource is determined. * @return Instance of an <code>Image</code>. */ public static Image getImageResourceFile(String strResourceFilename, Class srcClass) { PngImage pngImage = null; Image image = null; try { BufferedInputStream in = new BufferedInputStream(srcClass.getResourceAsStream(strResourceFilename)); if (in == null) { System.err.println("Image not found:" + strResourceFilename); return null; } if (strResourceFilename.endsWith(".png")) { pngImage = new PngImage(in); image = Toolkit.getDefaultToolkit().createImage(pngImage); } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); copy(in, out); image = Toolkit.getDefaultToolkit().createImage(out.toByteArray()); } } catch (java.io.IOException e) { System.err.println("Unable to read image " + strResourceFilename + "."); e.printStackTrace(); } return (image); }
/** * ** Reads the bytes from the specifed socket until an eod-of-stream error occurs, or ** until * the maximum number of bytes has bee read. ** @param socket The socket from which bytes are read * ** @param baos The ByteArrayOutputStream to which the bytes are written ** @param maxLength The * number of bytes to read from the socket ** @return The number of bytes read if no exception has * occurred ** @throws IOException if an error occured or the server has stopped */ protected static int socketReadBytes(Socket socket, ByteArrayOutputStream baos, int maxLength) throws IOException { if (socket == null) { return 0; } else if (maxLength == 0) { return 0; } else { int dataLen = 0; InputStream input = socket.getInputStream(); while ((maxLength < 0) || (dataLen < maxLength)) { int ch = input.read(); if (ch < 0) { // we've reached the end of input return dataLen; } else { if (baos != null) { baos.write(ch); } dataLen++; } } return dataLen; } }