/* * XImageBytes.java -- * * Copyright 1995 Regents of the University of California * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without * fee is hereby granted, provided that this copyright * notice appears in all copies. The University of California * makes no representations about the suitability of this * software for any purpose. It is provided "as is" without * express or implied warranty. * * rcsid = "$Header: /disks/barad-dur/now/rywang/src/java/classes/ryw/XVector.java,v 1.1 1995/11/09 12:19:39 rywang Exp $ xFS (Berkeley)" */ package ryw; import java.lang.*; import java.applet.*; import java.net.*; import java.io.*; import java.awt.*; import sun.net.www.protocol.memory.*; public class XImageBytes { static Applet applet = null; static int count = 0; static String prefix = null; public XImageBytes (Applet applet, String prefix) { this.applet = applet; this.prefix = prefix; } /* public Image bytes2imageOld (byte bytes[]) { XError.say ("bytes2image: entered..."); String tmpFileName = prefix+count; count++; XError.say ("bytes2image: writing tmp image..."); try { FileOutputStream outs = new FileOutputStream (tmpFileName); outs.write (bytes); outs.close (); } catch (Exception e) { XError.yell (e, "bytes2image: failed to write"); return null; } XError.say ("bytes2image: loading image..."); Image theImage; try { URL url = new URL ("file://"+tmpFileName); MediaTracker tracker = new MediaTracker (applet); theImage = applet.getImage (url); tracker.addImage (theImage, 0); tracker.waitForAll (); } catch (Exception e) { XError.yell (e, "bytes2image: failed to get image"); return null; } return theImage; } */ public Image bytes2image (byte bytes[], String format) { Image image; XError.say ("bytes2image: really new version entered..."); try { image = (Image) MemoryURL.getImage (applet, "memory://localhost/foo"+count+"."+format, bytes); count++; MediaTracker tracker = new MediaTracker (applet); tracker.addImage (image, 0); tracker.waitForAll (); } catch (Exception e) { image = null; XError.yell ("bytes2image: MemoryURL.getContent failed"); e.printStackTrace (); } return image; } public byte[] name2bytes (String imageName) { XError.say ("name2bytes: entered..."); File imageFile = new File (imageName); if (!imageFile.canRead ()) { XError.yell ("name2bytes: can't read "+imageName); return null; } long size = imageFile.length (); byte bytes[] = new byte [(int) size]; try { FileInputStream ins = new FileInputStream (imageFile); if (ins.read (bytes) != size) { XError.yell ("name2bytes: read short file!"); return null; } ins.close (); } catch (Exception e) { XError.yell (e, "name2bytes: failed to read"); return null; } return bytes; } }