KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > rmi > RmiOutputStream


1 package com.daffodilwoods.rmi;
2
3  import java.io.*;
4  import java.rmi.*;
5  import com.daffodilwoods.rmi.interfaces.*;
6 public class RmiOutputStream extends OutputStream{
7
8   _RmiOutputStream outputStream_interface;
9   private int BUFFER_SIZE = 1000;
10   private int position = 0;
11   private byte[] buff;
12
13   public RmiOutputStream(_RmiOutputStream outputStream_interface) {
14     this.outputStream_interface = outputStream_interface;
15   }
16
17   public void write(int b) throws IOException{
18         try {
19             writeBytes(new byte[]{(byte)b},0,1);
20         }
21         catch (RemoteException re) {
22             throw new RuntimeException JavaDoc(re.getMessage());
23         }
24   }
25
26   public void write(byte b[]) throws IOException{
27         try {
28           if(b == null)
29             return;
30           write(b,0,b.length);
31         }
32         catch (RemoteException re) {
33             throw new RuntimeException JavaDoc(re.getMessage());
34         }
35   }
36
37   public void write(byte b[], int off, int len) throws IOException{
38     ensureOpen();
39     if ( (off < 0) || (off > b.length) || (len < 0) ||
40         ( (off + len) > b.length) || ( (off + len) < 0)) {
41       throw new IndexOutOfBoundsException JavaDoc();
42     }
43     else if (len == 0) {
44       return;
45     }
46     writeBytes(b, off, len);
47   }
48
49   public void flush() throws IOException{
50         try {
51           flushBuffer();
52           outputStream_interface.flush();
53         }
54         catch (RemoteException re) {
55             throw new RuntimeException JavaDoc(re.getMessage());
56         }
57   }
58
59   public void close() throws IOException{
60         try {
61           flushBuffer();
62           outputStream_interface.close();
63         }
64         catch (RemoteException re) {
65             throw new RuntimeException JavaDoc(re.getMessage());
66         }
67   }
68
69   private void writeBytes(byte[] byte0,int off,int len) throws IOException{
70     try {
71       if (len >= BUFFER_SIZE) {
72           /* If the request length exceeds the size of the output buffer,
73              flush the buffer and then write the data directly. In this
74              way buffered streams will cascade harmlessly. */

75           flushBuffer();
76           outputStream_interface.write(byte0, off, len);
77           return;
78       }
79       int b = off, t = off + len;
80       while (b < t) {
81           int d = min(BUFFER_SIZE - position, t - b);
82           System.arraycopy(byte0, b, buff, position, d);
83           b += d;
84           position += d;
85           if (position >= BUFFER_SIZE)
86               flushBuffer();
87       }
88     }
89     catch (RemoteException re) {
90                     throw new RuntimeException JavaDoc(re.getMessage());
91     }
92   }
93
94   /**
95    * Our own little min method, to avoid loading java.lang.Math if we've run
96    * out of file descriptors and we're trying to print a stack trace.
97    */

98   private int min(int a, int b) {
99       if (a < b) return a;
100       return b;
101   }
102
103   private void flushBuffer() throws IOException{
104     ensureOpen();
105     if (position == 0)
106         return;
107     outputStream_interface.write(buff,0,position);
108     position = 0;
109   }
110
111   /** Check to make sure that the stream has not been closed */
112   private void ensureOpen() throws IOException {
113       if (outputStream_interface == null)
114           throw new IOException("Stream closed");
115   }
116
117   public void finalize(){
118     try {
119       flushBuffer();
120     }
121     catch (IOException ex) {
122     }
123   }
124
125 }
126
Popular Tags