KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > streams > SizeConstrainedInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core.streams;
12
13 import java.io.FilterInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InterruptedIOException JavaDoc;
17
18 import org.eclipse.core.runtime.OperationCanceledException;
19
20 /**
21  * Simulates a stream that represents only a portion of the underlying stream.
22  * Will report EOF when this portion has been fully read and prevent further reads.
23  * The underlying stream is not closed on close(), but the remaining unread input
24  * may optionally be skip()'d.
25  *
26  * Supports resuming partially completed operations after an InterruptedIOException
27  * if the underlying stream does. Check the bytesTransferred field to determine how
28  * much of the operation completed; conversely, at what point to resume.
29  */

30 public class SizeConstrainedInputStream extends FilterInputStream JavaDoc {
31     private boolean discardOnClose;
32     private long bytesRemaining;
33     
34     /**
35      * Creates a size contrained input stream.
36      * @param in the underlying input stream, never actually closed by this filter
37      * @param size the maximum number of bytes of the underlying input stream that
38      * can be read through this filter
39      * @param discardOnClose if true, discards remaining unread bytes on close()
40      */

41     public SizeConstrainedInputStream(InputStream JavaDoc in, long size, boolean discardOnClose) {
42         super(in);
43         this.bytesRemaining = size;
44         this.discardOnClose = discardOnClose;
45     }
46     
47     /**
48      * Prevents further reading from the stream but does not close the underlying stream.
49      * If discardOnClose, skip()'s over any remaining unread bytes in the constrained region.
50      * @throws IOException if an i/o error occurs
51      */

52     public void close() throws IOException JavaDoc {
53         try {
54             if (discardOnClose) {
55                 while (bytesRemaining != 0 && skip(bytesRemaining) != 0);
56             }
57         } catch (OperationCanceledException e) {
58             // The receiver is likely wrapping a PollingInputStream which could throw
59
// an OperationCanceledException on a skip.
60
// Since we're closing, just ignore the cancel and let the caller check the monitor
61
} finally {
62             bytesRemaining = 0;
63         }
64     }
65
66     /**
67      * Wraps the underlying stream's method.
68      * Simulates an end-of-file condition if the end of the constrained region has been reached.
69      * @throws IOException if an i/o error occurs
70      */

71     public int available() throws IOException JavaDoc {
72         int amount = in.available();
73         if (amount > bytesRemaining) amount = (int) bytesRemaining;
74         return amount;
75     }
76     
77     /**
78      * Wraps the underlying stream's method.
79      * Simulates an end-of-file condition if the end of the constrained region has been reached.
80      * @throws InterruptedIOException if the operation was interrupted before all of the
81      * bytes specified have been skipped, bytesTransferred will be zero
82      * @throws IOException if an i/o error occurs
83      */

84     public int read() throws IOException JavaDoc {
85         if (bytesRemaining == 0) return -1;
86         int b = in.read();
87         if (b != -1) bytesRemaining -= 1;
88         return b;
89     }
90     
91     /**
92      * Wraps the underlying stream's method.
93      * Simulates an end-of-file condition if the end of the constrained region has been reached.
94      * @throws InterruptedIOException if the operation was interrupted before all of the
95      * bytes specified have been skipped, bytesTransferred may be non-zero
96      * @throws IOException if an i/o error occurs
97      */

98     public int read(byte[] buffer, int offset, int length) throws IOException JavaDoc {
99         if (length > bytesRemaining) {
100             if (bytesRemaining == 0) return -1;
101             length = (int) bytesRemaining;
102         }
103         try {
104             int count = in.read(buffer, offset, length);
105             if (count != -1) bytesRemaining -= count;
106             return count;
107         } catch (InterruptedIOException JavaDoc e) {
108             bytesRemaining -= e.bytesTransferred;
109             throw e;
110         }
111     }
112     
113     /**
114      * Wraps the underlying stream's method.
115      * Simulates an end-of-file condition if the end of the constrained region has been reached.
116      * @throws InterruptedIOException if the operation was interrupted before all of the
117      * bytes specified have been skipped, bytesTransferred may be non-zero
118      * @throws IOException if an i/o error occurs
119      */

120     public long skip(long amount) throws IOException JavaDoc {
121         if (amount > bytesRemaining) amount = bytesRemaining;
122         try {
123             long count = in.skip(amount);
124             bytesRemaining -= count;
125             return count;
126         } catch (InterruptedIOException JavaDoc e) {
127             bytesRemaining -= e.bytesTransferred;
128             throw e;
129         }
130     }
131     
132     /**
133      * Mark is not supported by the wrapper even if the underlying stream does, returns false.
134      */

135     public boolean markSupported() {
136         return false;
137     }
138 }
139
Popular Tags