Пример #1
0
  public void run() {

    byte[] tmp = new byte[10000];
    int read;
    try {
      FileInputStream fis = new FileInputStream(fileToSend.getFile());

      read = fis.read(tmp);
      while (read != -1) {
        baos.write(tmp, 0, read);
        read = fis.read(tmp);
        System.out.println(read);
      }
      fis.close();
      baos.writeTo(sWriter);
      sWriter.flush();
      baos.flush();
      baos.close();
      System.out.println("fileSent");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

      this.closeSocket();
    }
  }
  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);
  }
 private ByteBuffer buffer(Message m[]) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   for (int i = 0; i < m.length; i++) m[i].write(out);
   out.flush();
   return ByteBuffer.wrap(out.toByteArray());
 }
 private ByteBuffer buffer(List ms) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   for (Iterator i = ms.iterator(); i.hasNext(); ) ((Message) i.next()).write(out);
   out.flush();
   return ByteBuffer.wrap(out.toByteArray());
 }
 private ByteBuffer buffer(Message m) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   m.write(out);
   out.flush();
   return ByteBuffer.wrap(out.toByteArray());
 }