KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.rmi;
2
3  import java.io.*;
4  import java.rmi.*;
5  import com.daffodilwoods.rmi.interfaces.*;
6 public class RmiWriter extends Writer {
7
8   _RmiWriter writer_interface;
9   private int BUFFER_SIZE = 1000;
10   private int position = 0;
11   private char[] buff;
12
13   public RmiWriter(_RmiWriter writer_interface) {
14       this.writer_interface = writer_interface;
15       buff = new char[BUFFER_SIZE];
16       position = 0;
17   }
18
19     public void write(int c) throws IOException {
20         try {
21             writechars(new char[]{(char)c},0,1);
22         }
23         catch (RemoteException re) {
24             throw new RuntimeException JavaDoc(re.getMessage());
25         }
26     }
27
28
29     public void write(char cbuf[]) throws IOException{
30         try {
31           if(cbuf == null)
32             return;
33           writechars(cbuf,0,cbuf.length);
34         }
35         catch (RemoteException re) {
36             throw new RuntimeException JavaDoc(re.getMessage());
37         }
38     }
39     public void write(char cbuf[], int off, int len) throws IOException{
40       ensureOpen();
41       if ((off < 0) || (off > cbuf.length) || (len < 0) ||
42           ((off + len) > cbuf.length) || ((off + len) < 0)) {
43           throw new IndexOutOfBoundsException JavaDoc();
44       } else if (len == 0) {
45           return;
46       }
47       writechars(cbuf,off,len);
48     }
49     public void write(String JavaDoc str) throws IOException {
50         try {
51           if(str == null)
52             return;
53           char[] chs = str.toCharArray();
54           writechars(chs,0,chs.length);
55         }
56         catch (RemoteException re) {
57             throw new RuntimeException JavaDoc(re.getMessage());
58         }
59     }
60     public void write(String JavaDoc str, int off, int len) throws IOException {
61
62       try {
63         if(str == null)
64           throw new NullPointerException JavaDoc();
65         char[] chs = str.toCharArray();
66         write(chs,0,chs.length);
67         }
68         catch (RemoteException re) {
69             throw new RuntimeException JavaDoc(re.getMessage());
70         }
71     }
72
73     public void flush() throws IOException{
74         try {
75           flushBuffer();
76             writer_interface.flush();
77         }
78         catch (RemoteException re) {
79             throw new RuntimeException JavaDoc(re.getMessage());
80         }
81     }
82     public void close() throws IOException{
83         try {
84           flushBuffer();
85           writer_interface.close();
86         }
87         catch (RemoteException re) {
88             throw new RuntimeException JavaDoc(re.getMessage());
89         }
90     }
91
92     private void writechars(char[] cbuf,int off,int len) throws IOException{
93       try {
94         if (len >= BUFFER_SIZE) {
95             /* If the request length exceeds the size of the output buffer,
96                flush the buffer and then write the data directly. In this
97                way buffered streams will cascade harmlessly. */

98             flushBuffer();
99             writer_interface.writeChars(cbuf, off, len);
100             return;
101         }
102         int b = off, t = off + len;
103         while (b < t) {
104             int d = min(BUFFER_SIZE - position, t - b);
105             System.arraycopy(cbuf, b, buff, position, d);
106             b += d;
107             position += d;
108             if (position >= BUFFER_SIZE)
109                 flushBuffer();
110         }
111       }
112       catch (RemoteException re) {
113                       throw new RuntimeException JavaDoc(re.getMessage());
114       }
115     }
116
117     /**
118      * Our own little min method, to avoid loading java.lang.Math if we've run
119      * out of file descriptors and we're trying to print a stack trace.
120      */

121     private int min(int a, int b) {
122         if (a < b) return a;
123         return b;
124     }
125
126     private void flushBuffer() throws IOException{
127       ensureOpen();
128       if (position == 0)
129           return;
130       writer_interface.writeChars(buff,0,position);
131       position = 0;
132     }
133
134     /** Check to make sure that the stream has not been closed */
135     private void ensureOpen() throws IOException {
136         if (writer_interface == null)
137             throw new IOException("Stream closed");
138     }
139
140     public void finalize(){
141       try {
142         flushBuffer();
143       }
144       catch (IOException ex) {
145       }
146     }
147 }
148
Popular Tags