Debian Codegraph @

jetty-infinispan/infinispan-common/src/main/java/org/eclipse/jetty/session/infinispan/BoundDelegatingInputStream.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.session.infinispan;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.ObjectInput;
24
25 /**
26 * BoundDelegatingInputStream
27 *
28 * An InputStream that delegates methods to an ObjectInput. The ObjectInput must start
29 * with an integer containing the length of the data.
30 */
31 public class BoundDelegatingInputStream extends InputStream
32 {
33
34 protected final ObjectInput objectInput;
35 private final int length;
36 private int position = 0;
37
38 public BoundDelegatingInputStream(ObjectInput objectInput) throws IOException
39 {
40 this.objectInput = objectInput;
41 this.length = objectInput.readInt();
42 }
43
44 @Override
45 public int read() throws IOException
46 {
47 if (position < length)
48 {
49 position++;
50 return objectInput.read();
51 }
52 return -1;
53 }
54
55 @Override
56 public int read(byte[] b) throws IOException
57 {
58 int available = length - position;
59 int read = -1;
60 if (position == length)
61 {
62 return read;
63 }
64 if (b.length > available)
65 {
66 read = objectInput.read(b, 0, available);
67 }
68 else
69 {
70 read = objectInput.read(b);
71 }
72 if (read != -1)
73 {
74 position += read;
75 }
76 return read;
77 }
78
79 @Override
80 public int read(byte[] b, int off, int len) throws IOException
81 {
82 int read = -1;
83 if (position == length)
84 {
85 return read;
86 }
87 if (position + len > length)
88 {
89 read = objectInput.read(b, off, length - position);
90 }
91 else
92 {
93 read = objectInput.read(b, off, len);
94 }
95 if (read != -1)
96 {
97 position += read;
98 }
99 return read;
100 }
101
102 @Override
103 public long skip(long n) throws IOException
104 {
105 long skip = 0;
106 if (position + n < length)
107 {
108 skip = objectInput.skip(length - position);
109 }
110 else
111 {
112 skip = objectInput.skip(n);
113 }
114 if (skip > 0)
115 {
116 position += skip;
117 }
118 return skip;
119 }
120
121 @Override
122 public int available() throws IOException
123 {
124 if (position < length)
125 {
126 int available = objectInput.available();
127 if (position + available > length)
128 {
129 return length - position;
130 }
131 else
132 {
133 return available;
134 }
135 }
136 return 0;
137 }
138
139 @Override
140 public void close() throws IOException
141 {
142 objectInput.close();
143 }
144
145 }