KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > streams > JMXChunkedOutputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.jmx.remote.streams;
25
26 import java.io.IOException JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 public class JMXChunkedOutputStream extends OutputStream JavaDoc {
33     private OutputStream JavaDoc out = null;
34     private byte[] buffer = null;
35     private int bufCount = 0;
36
37     public JMXChunkedOutputStream(OutputStream JavaDoc out) {
38         this.out = out;
39         buffer = new byte[8192];
40     }
41
42     public void close() throws IOException JavaDoc {
43         if (bufCount > 0)
44             flush();
45         out.close();
46     }
47
48     public void flush() throws IOException JavaDoc {
49         if (bufCount > 0)
50             flushBuffer();
51         else
52             out.flush();
53     }
54
55     private void flushBuffer() throws IOException JavaDoc {
56         writeObject(buffer, 0, bufCount);
57         bufCount = 0;
58     }
59
60     public void writeEOF(int padLen) throws IOException JavaDoc {
61         DataOutputStream JavaDoc dO = new DataOutputStream JavaDoc(out);
62         dO.writeInt(0);
63         // Kludge:: For some wierd reason, the StreamingOutputStream of
64
// HttpURLConnection is not counting the requestmessage object's
65
// length as the number of bytes written.
66
// Hence, we will send some padding bytes at the end to fool
67
// StreamingOutputStream.
68
dO.write(new byte[padLen],0,padLen);
69         dO.flush();
70     }
71
72     public void write(byte[] b) throws IOException JavaDoc {
73         if (b == null)
74             throw (new NullPointerException JavaDoc("byte array is null"));
75         write(b, 0, b.length);
76     }
77
78     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
79         if (b == null)
80             throw (new NullPointerException JavaDoc("byte array is null"));
81         if (off < 0 || len < 0 || (off+len) > b.length)
82             throw (new IndexOutOfBoundsException JavaDoc(
83                                     "offset="+off+
84                                     ", len="+len+
85                                     ", (off+len)="+(off+len)+
86                                     ", b.length="+b.length+
87                                     ", (off+len)>b.length="+
88                                         ((off+len)>b.length)));
89         if (len == 0)
90             return;
91         if (bufCount > 0 && (bufCount+len) >= 8192) {
92             flushBuffer();
93         }
94         if (len >= 8192) {
95             writeObject(b, off, len);
96             return;
97         }
98         writeBuffer(b, off, len);
99     }
100
101     public void write(int by) throws IOException JavaDoc {
102         byte b = (byte) by;
103         if (bufCount > 0 && (bufCount+1) >= 8192) {
104             flushBuffer();
105         }
106         buffer[bufCount] = b;
107         bufCount++;
108     }
109
110     private void writeBuffer(byte[] b, int off, int len) {
111         System.arraycopy(b, off, buffer, bufCount, len);
112         bufCount += len;
113     }
114
115     private void writeObject(byte[] b, int off, int len)
116             throws IOException JavaDoc {
117         DataOutputStream JavaDoc dO = new DataOutputStream JavaDoc(out);
118         dO.writeInt(len);
119         dO.write(b, off, len);
120         dO.flush();
121     }
122 }
123
124
Popular Tags