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 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 } }
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); }