home *** CD-ROM | disk | FTP | other *** search
/ PC User 2004 August / Disc 1 / PCU0804CD1.iso / software / browsers / files / opera1.exe / OPERA.JAR / com / opera / OperaOutputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-02-12  |  2.5 KB  |  105 lines

  1. package com.opera;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7.  
  8. class OperaOutputStream extends OutputStream {
  9.    private static final int BUFFER_SIZE = 16384;
  10.    private static Hashtable stream_map = new Hashtable();
  11.    private static int next_stream_id = 0;
  12.    private byte[] buffer = new byte[16384];
  13.    private int next_write_byte = 0;
  14.    private int stream_id = -1;
  15.    protected int istream_id = -1;
  16.    private boolean is_closed = false;
  17.    protected Hashtable req_props = new Hashtable();
  18.  
  19.    protected OperaOutputStream() {
  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.          this.flushBuffer();
  37.       }
  38.    }
  39.  
  40.    public void close() throws IOException {
  41.       if (this.buffer != null) {
  42.          this.flushBuffer();
  43.          this.postData(this.istream_id, this.makeExtraHeaders());
  44.          this.buffer = null;
  45.          this.is_closed = true;
  46.       }
  47.  
  48.    }
  49.  
  50.    boolean isClosed() {
  51.       return this.is_closed;
  52.    }
  53.  
  54.    private void flushBuffer() {
  55.       if (this.next_write_byte > 0) {
  56.          this.addData(this.buffer, this.next_write_byte);
  57.          this.next_write_byte = 0;
  58.       }
  59.  
  60.    }
  61.  
  62.    private native void postData(int var1, String var2);
  63.  
  64.    private native void addData(byte[] var1, int var2);
  65.  
  66.    protected static int addStream(OperaOutputStream var0) {
  67.       synchronized(stream_map) {
  68.          stream_map.put(new Integer(next_stream_id), var0);
  69.          var0.stream_id = next_stream_id++;
  70.       }
  71.  
  72.       return var0.stream_id;
  73.    }
  74.  
  75.    protected static void removeStream(int var0) {
  76.       synchronized(stream_map) {
  77.          stream_map.remove(new Integer(var0));
  78.       }
  79.    }
  80.  
  81.    protected static OperaOutputStream getStream(int var0) {
  82.       synchronized(stream_map) {
  83.          return (OperaOutputStream)stream_map.get(new Integer(var0));
  84.       }
  85.    }
  86.  
  87.    private String makeExtraHeaders() {
  88.       String var1 = "";
  89.       Enumeration var2 = this.req_props.keys();
  90.       Enumeration var3 = this.req_props.elements();
  91.  
  92.       while(var2.hasMoreElements()) {
  93.          var1 = var1.concat((String)var2.nextElement());
  94.          String var4 = (String)var3.nextElement();
  95.          if (var4 != null) {
  96.             var1 = var1.concat(": " + var4 + "\r\n");
  97.          } else {
  98.             var1 = var1.concat("\r\n");
  99.          }
  100.       }
  101.  
  102.       return var1;
  103.    }
  104. }
  105.