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);
  }
Exemplo n.º 2
0
  /**
   * 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();
    }
  }
Exemplo n.º 3
0
  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;
  }
Exemplo n.º 4
0
 /**
  * ** 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;
   }
 }