/* * XImageBytes.java -- * * Copyright 1997 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.*; /** * Converts a file name to an Image, and * converts a bunch of raw bytes to an Image. */ public class XImageBytes { Applet applet; int count; String host; public XImageBytes () {} public XImageBytes (Applet applet, String host) { this.applet = applet; this.host = host; } public Image bytes2image (byte bytes[], String format) { Image image; try { image = (Image) MemoryURL.getImage (applet, "memory://"+host+"/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) { 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; } }