KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.DataInputStream JavaDoc;
29
30 public class JMXChunkedInputStream extends InputStream JavaDoc {
31     private InputStream JavaDoc in = null;
32     private byte[] buffer = null;
33     private int remainderInBuf = 0;
34     private int remainderInChannel = 0;
35     private int idx = 0;
36     private boolean EOF = false;
37
38     public JMXChunkedInputStream(InputStream JavaDoc in) {
39         this.in = in;
40     }
41
42     public void close() throws IOException JavaDoc {
43         in.close();
44     }
45
46     public int available() {
47         return remainderInBuf;
48     }
49
50     public boolean markSupported() {
51         return false;
52     }
53
54     public void mark(int readLimit) {
55         return;
56     }
57
58     public void reset() throws IOException JavaDoc {
59         throw (new IOException JavaDoc("markSupported = false"));
60     }
61
62     private void readObject() throws IOException JavaDoc {
63         try {
64             DataInputStream JavaDoc dI = new DataInputStream JavaDoc(in);
65             int len = 0;
66             if (remainderInChannel > 0)
67                 len = remainderInChannel;
68             else {
69                 remainderInChannel = len = dI.readInt();
70                 if (len <= 0) {
71                     setEOF();
72                     return;
73                 }
74             }
75
76             buffer = new byte[len];
77             remainderInBuf = dI.read(buffer, 0, len);
78             if (remainderInBuf == -1) {
79                 setEOF();
80                 return;
81             }
82             remainderInChannel -= remainderInBuf;
83             idx = 0;
84         } catch (IOException JavaDoc ioe) {
85             if (ioe instanceof java.io.EOFException JavaDoc ||
86                 ioe.getMessage().indexOf("EOF") != -1) {
87                 setEOF();
88                 return;
89             } else
90                 throw ioe;
91         }
92     }
93
94     private void setEOF() {
95         EOF = true;
96         buffer = null;
97         remainderInBuf = 0;
98         idx = 0;
99     }
100
101     public int read() throws IOException JavaDoc {
102         if (EOF)
103             return -1;
104         byte ret = -1;
105         if (remainderInBuf == 0)
106             readObject();
107         if (EOF)
108             return -1;
109         ret = buffer[idx++];
110         remainderInBuf--;
111         return ret;
112     }
113
114     public int read(byte[] b) throws IOException JavaDoc {
115         if (b == null)
116             throw (new NullPointerException JavaDoc("byte array is null"));
117         return read(b, 0, b.length);
118     }
119
120     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
121         if (EOF)
122             return -1;
123         if (b == null)
124             throw (new NullPointerException JavaDoc("byte array is null"));
125         if (off < 0 || len < 0 || (off+len) > b.length)
126             throw (new IndexOutOfBoundsException JavaDoc(
127                                     "offset="+off+
128                                     ", len="+len+
129                                     ", (off+len)="+(off+len)+
130                                     ", b.length="+b.length+
131                                     ", (off+len)>b.length="+
132                                         ((off+len)>b.length)));
133         if (len == 0)
134             return 0;
135
136         if (remainderInBuf == 0)
137             readObject();
138         if (EOF)
139             return -1;
140         int len1 = (len > remainderInBuf) ? remainderInBuf : len;
141         System.arraycopy(buffer, idx, b, off, len1);
142         idx += len1;
143         remainderInBuf -= len1;
144         return len1;
145     }
146
147     public long skip(int n) throws IOException JavaDoc {
148         if (EOF)
149             return 0;
150         if (n <= 0)
151             return 0;
152         if (remainderInBuf >= n) {
153             idx += n;
154             remainderInBuf -= n;
155             return n;
156         }
157         int skipped = remainderInBuf;
158         n -= skipped;
159         remainderInBuf = 0;
160         idx = 0;
161         readObject();
162         if (EOF)
163             return skipped;
164         idx += n;
165         remainderInBuf -= n;
166         skipped += n;
167         return skipped;
168     }
169 }
170
171
Popular Tags