1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.util.byteaccess;
21
22
23 import org.apache.mina.core.buffer.IoBuffer;
24
25
26 /**
27 * Provides restricted, relative, read-only access to the bytes in a
28 * <code>CompositeByteArray</code>. Using this interface has the advantage
29 * that it can be automatically determined when a component
30 * <code>ByteArray</code> can no longer be read, and thus components can be
31 * automatically freed. This makes it easier to use pooling for underlying
32 * <code>ByteArray</code>s.
33 *
34 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35 */
36 public class CompositeByteArrayRelativeReader extends CompositeByteArrayRelativeBase implements IoRelativeReader
37 {
38
39 /**
40 * Whether or not to free component <code>CompositeByteArray</code>s when
41 * the cursor moves past them.
42 */
43 private final boolean autoFree;
44
45 /**
46 *
47 * Creates a new instance of CompositeByteArrayRelativeReader.
48 *
49 * @param cba
50 * The backing ByteArray
51 * @param autoFree
52 * If data should be freed once it has been passed in the list
53 */
54 public CompositeByteArrayRelativeReader( CompositeByteArray cba, boolean autoFree )
55 {
56 super( cba );
57 this.autoFree = autoFree;
58 }
59
60
61 @Override
62 protected void cursorPassedFirstComponent()
63 {
64 if ( autoFree )
65 {
66 cba.removeFirst().free();
67 }
68 }
69
70
71 /**
72 * @inheritDoc
73 */
74 public void skip( int length )
75 {
76 cursor.skip( length );
77 }
78
79
80 /**
81 * @inheritDoc
82 */
83 public ByteArray slice( int length )
84 {
85 return cursor.slice( length );
86 }
87
88 /**
89 * Returns the byte at the current position in the buffer
90 *
91 */
92 public byte get()
93 {
94 return cursor.get();
95 }
96
97 /**
98 * places the data starting at current position into the
99 * supplied {@link IoBuffer}
100 */
101 public void get( IoBuffer bb )
102 {
103 cursor.get( bb );
104 }
105
106
107 /**
108 * @inheritDoc
109 */
110 public short getShort()
111 {
112 return cursor.getShort();
113 }
114
115
116 /**
117 * @inheritDoc
118 */
119 public int getInt()
120 {
121 return cursor.getInt();
122 }
123
124
125 /**
126 * @inheritDoc
127 */
128 public long getLong()
129 {
130 return cursor.getLong();
131 }
132
133
134 /**
135 * @inheritDoc
136 */
137 public float getFloat()
138 {
139 return cursor.getFloat();
140 }
141
142
143 /**
144 * @inheritDoc
145 */
146 public double getDouble()
147 {
148 return cursor.getDouble();
149 }
150
151
152 /**
153 * @inheritDoc
154 */
155 public char getChar()
156 {
157 return cursor.getChar();
158 }
159
160 }