home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2003 January / maximum-cd-2003-01.iso / Software / Apps / OperawithJava / ow32enen700b1j.exe / OPERA.JAR / opera / OperaOutputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-11-05  |  2.1 KB  |  95 lines

  1. package opera;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Hashtable;
  6.  
  7. class OperaOutputStream extends OutputStream {
  8.    private static final int BUFFER_SIZE = 16384;
  9.    private static Hashtable stream_map = new Hashtable();
  10.    private static int next_stream_id = 0;
  11.    private byte[] buffer = new byte[16384];
  12.    private int next_write_byte = 0;
  13.    private int stream_id = -1;
  14.    protected int istream_id = -1;
  15.    private OperaURLConnection connection = null;
  16.    private boolean is_closed = false;
  17.  
  18.    protected OperaOutputStream(OperaURLConnection var1) {
  19.       this.connection = var1;
  20.    }
  21.  
  22.    public void write(int var1) throws IOException {
  23.       if (this.buffer == null) {
  24.          throw new IOException();
  25.       } else {
  26.          this.buffer[this.next_write_byte++] = (byte)var1;
  27.          if (this.next_write_byte == 16384) {
  28.             this.flushBuffer();
  29.          }
  30.  
  31.       }
  32.    }
  33.  
  34.    public void flush() throws IOException {
  35.       if (this.buffer == null) {
  36.          throw new IOException();
  37.       } else {
  38.          this.flushBuffer();
  39.       }
  40.    }
  41.  
  42.    public void close() throws IOException {
  43.       if (this.buffer != null) {
  44.          this.flushBuffer();
  45.          this.postData(this.istream_id, this.connection.makeExtraHeaders());
  46.          this.connection = null;
  47.          this.buffer = null;
  48.          this.is_closed = true;
  49.       } else {
  50.          throw new IOException();
  51.       }
  52.    }
  53.  
  54.    boolean isClosed() {
  55.       return this.is_closed;
  56.    }
  57.  
  58.    private void flushBuffer() {
  59.       if (this.next_write_byte > 0) {
  60.          this.addData(this.buffer, this.next_write_byte);
  61.          this.next_write_byte = 0;
  62.       }
  63.  
  64.    }
  65.  
  66.    private native void postData(int var1, String var2);
  67.  
  68.    private native void addData(byte[] var1, int var2);
  69.  
  70.    protected static int addStream(OperaOutputStream var0) {
  71.       Hashtable var1 = stream_map;
  72.       synchronized(var1) {
  73.          stream_map.put(new Integer(next_stream_id), var0);
  74.          var0.stream_id = next_stream_id++;
  75.       }
  76.  
  77.       return var0.stream_id;
  78.    }
  79.  
  80.    protected static void removeStream(int var0) {
  81.       Hashtable var1 = stream_map;
  82.       synchronized(var1) {
  83.          stream_map.remove(new Integer(var0));
  84.       }
  85.    }
  86.  
  87.    protected static OperaOutputStream getStream(int var0) {
  88.       Hashtable var1 = stream_map;
  89.       synchronized(var1) {
  90.          OperaOutputStream var2 = (OperaOutputStream)stream_map.get(new Integer(var0));
  91.          return var2;
  92.       }
  93.    }
  94. }
  95.