~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet/src/org/restlet/engine/http/io/SizedInputStream.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.engine.http.io;
 
35
 
 
36
import java.io.IOException;
 
37
import java.io.InputStream;
 
38
 
 
39
// [excludes gwt]
 
40
/**
 
41
 * Input stream based on a source stream that must only be partially read.
 
42
 * 
 
43
 * @author Jerome Louvel
 
44
 */
 
45
public class SizedInputStream extends InputEntityStream {
 
46
 
 
47
    /** The total size that should be read from the source stream. */
 
48
    private volatile long availableSize;
 
49
 
 
50
    /** The total size when the {@link #mark(int)} method was called. */
 
51
    private volatile long markedAvailableSize;
 
52
 
 
53
    /**
 
54
     * Constructor.
 
55
     * 
 
56
     * @param notifiable
 
57
     *            The notifiable connection.
 
58
     * @param inboundStream
 
59
     *            The inbound stream.
 
60
     * @param size
 
61
     *            The total size that should be read from the source stream.
 
62
     */
 
63
    public SizedInputStream(Notifiable notifiable, InputStream inboundStream,
 
64
            long size) {
 
65
        super(notifiable, inboundStream);
 
66
        this.availableSize = size;
 
67
        this.markedAvailableSize = -1;
 
68
    }
 
69
 
 
70
    @Override
 
71
    public int available() throws IOException {
 
72
        return Math.min((int) this.availableSize, getInboundStream()
 
73
                .available());
 
74
    }
 
75
 
 
76
    @Override
 
77
    public void close() throws IOException {
 
78
        // Don't close it directly
 
79
    }
 
80
 
 
81
    @Override
 
82
    public synchronized void mark(int readlimit) {
 
83
        if (markSupported()) {
 
84
            this.markedAvailableSize = availableSize;
 
85
        }
 
86
 
 
87
        getInboundStream().mark(readlimit);
 
88
    }
 
89
 
 
90
    @Override
 
91
    public boolean markSupported() {
 
92
        return getInboundStream().markSupported();
 
93
    }
 
94
 
 
95
    /**
 
96
     * Reads a byte from the underlying stream.
 
97
     * 
 
98
     * @return The byte read, or -1 if the end of the stream has been reached.
 
99
     */
 
100
    @Override
 
101
    public int read() throws IOException {
 
102
        int result = -1;
 
103
 
 
104
        if (this.availableSize > 0) {
 
105
            result = getInboundStream().read();
 
106
 
 
107
            if (result != -1) {
 
108
                this.availableSize--;
 
109
            } else {
 
110
                onEndReached();
 
111
            }
 
112
        }
 
113
 
 
114
        // The stream has been fully read.
 
115
        if (this.availableSize <= 0) {
 
116
            onEndReached();
 
117
        }
 
118
 
 
119
        return result;
 
120
    }
 
121
 
 
122
    @Override
 
123
    public int read(byte b[], int off, int len) throws IOException {
 
124
        int result = -1;
 
125
 
 
126
        if (this.availableSize > 0) {
 
127
            result = getInboundStream().read(b, off,
 
128
                    Math.min(len, (int) this.availableSize));
 
129
 
 
130
            if (result > 0) {
 
131
                this.availableSize -= result;
 
132
            } else if (result == -1) {
 
133
                onEndReached();
 
134
            }
 
135
        }
 
136
 
 
137
        if (this.availableSize <= 0) {
 
138
            onEndReached();
 
139
        }
 
140
 
 
141
        return result;
 
142
    }
 
143
 
 
144
    @Override
 
145
    public synchronized void reset() throws IOException {
 
146
        if (markSupported()) {
 
147
            if (this.markedAvailableSize != -1) {
 
148
                this.availableSize = markedAvailableSize;
 
149
                this.markedAvailableSize = -1;
 
150
            }
 
151
        }
 
152
 
 
153
        getInboundStream().reset();
 
154
    }
 
155
 
 
156
    @Override
 
157
    public long skip(long n) throws IOException {
 
158
        return getInboundStream().skip(n);
 
159
    }
 
160
 
 
161
}