summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/ImageSender.java
blob: c1659267537b3850b991fb9d631bb5d764bf153e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package imaging.framework;

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.*;
import JACE.OS.*;
import JACE.Connection.*;

public class ImageSender 
{
  public ImageSender (Applet parent)
  {
    this.parent_ = parent;
  }

  // ****************** 
  // Currently bombs if it's not in an http or / format
  //
  public void open (Image image, String url)
  {
    this.image_ = image;

    this.hostname_ = url;
    if (this.hostname_.startsWith ("http://"))
      this.hostname_ = this.hostname_.substring (7);

    int index = -1;
    if ((index = this.hostname_.indexOf (":")) != -1)
      {
	String temp = this.hostname_.substring (index + 1);
	int i = -1;
	if ((i = temp.indexOf ("/")) != -1)
	  {
	    this.port_ = (new Integer (temp.substring (0, i))).intValue ();
	    this.filename_ = temp.substring (i);
	  }
	this.hostname_ = this.hostname_.substring (0, index);
      }
    else
      {
	int i = -1;
	if ((i = this.hostname_.indexOf ("/")) != -1)
	  {
	    this.filename_ = hostname_.substring (i);
	  }
	this.hostname_ = this.hostname_.substring (0, i);
	this.port_ = 80;
      }
  }

  // Maintain compatibility 
  public int send ()
    {
      return send(null);
    }

  /*******************************************
   * The authenication string allows the PUT command to work if the server
   * accepts the basic HTTP 1.1 encryption scheme
   *******************************************/
  public int send (String authentication)
  {
    ImageByteCounter byteCounter = new ImageByteCounter ("", this.image_, this.parent_);
    //    StatusIndicator indicator = new StatusIndicator ("");
    //    ImageByteCounter byteCounter = new ImageByteCounter ("", this.image_, indicator);
    int length = byteCounter.count ();
    //    indicator.dispose ();
    // First determine how many bytes we will be sending
    /*
    try
      {
	GIFOutputStream ostream = new GIFOutputStream ();
	GifEncoder encoder = new GifEncoder (this.image_, ostream);
	encoder.encode ();

	length = ostream.count ();
	System.out.println ("send: " + length);
      }
    catch (IOException e)
      {
	ACE.ERROR ("Exception generating gif");
      }
      */

    GIFHandler gifHandler;
    if (authentication == null)
      gifHandler = new GIFHandler (this.filename_, this.image_, length);
    else
      gifHandler = new GIFHandler (this.filename_, this.image_, length, authentication);

    try
      {
	// Connect to the server and send the image
	this.connector_.open (this.hostname_, this.port_);
	this.connector_.connect (gifHandler);
      }
    catch (UnknownHostException e)
      {
	ACE.ERROR (e);
      }
    catch (InstantiationException e)
      {
	ACE.ERROR (e);
      }
    catch (IllegalAccessException e)
      {
	ACE.ERROR (e);
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }

    if (gifHandler.returnCode () != -1)
      return gifHandler.bytesWritten ();
    else
      return -1;
  }

  private Image image_ = null;
  String filename_;
  String hostname_;
  int port_;
  Connector connector_ = new Connector ();
  Applet parent_;
}