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 import org.apache.mina.core.buffer.IoBuffer;
23
24 /**
25 * Provides restricted, relative, read-only access to the bytes in a
26 * <code>CompositeByteArray</code>. Using this interface has the advantage
27 * that it can be automatically determined when a component
28 * <code>ByteArray</code> can no longer be read, and thus components can be
29 * automatically freed. This makes it easier to use pooling for underlying
30 * <code>ByteArray</code>s.
31 *
32 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33 */
34 public class CompositeByteArrayRelativeReader extends CompositeByteArrayRelativeBase implements IoRelativeReader {
35
36 /**
37 * Whether or not to free component <code>CompositeByteArray</code>s when
38 * the cursor moves past them.
39 */
40 private final boolean autoFree;
41
42 /**
43 *
44 * Creates a new instance of CompositeByteArrayRelativeReader.
45 *
46 * @param cba
47 * The backing ByteArray
48 * @param autoFree
49 * If data should be freed once it has been passed in the list
50 */
51 public CompositeByteArrayRelativeReader(CompositeByteArray cba, boolean autoFree) {
52 super(cba);
53 this.autoFree = autoFree;
54 }
55
56 @Override
57 protected void cursorPassedFirstComponent() {
58 if (autoFree) {
59 cba.removeFirst().free();
60 }
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public void skip(int length) {
67 cursor.skip(length);
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public ByteArray slice(int length) {
74 return cursor.slice(length);
75 }
76
77 /**
78 * Returns the byte at the current position in the buffer
79 *
80 */
81 public byte get() {
82 return cursor.get();
83 }
84
85 /**
86 * places the data starting at current position into the
87 * supplied {@link IoBuffer}
88 */
89 public void get(IoBuffer bb) {
90 cursor.get(bb);
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public short getShort() {
97 return cursor.getShort();
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public int getInt() {
104 return cursor.getInt();
105 }
106
107 /**
108 * @inheritDoc
109 */
110 public long getLong() {
111 return cursor.getLong();
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public float getFloat() {
118 return cursor.getFloat();
119 }
120
121 /**
122 * @inheritDoc
123 */
124 public double getDouble() {
125 return cursor.getDouble();
126 }
127
128 /**
129 * @inheritDoc
130 */
131 public char getChar() {
132 return cursor.getChar();
133 }
134
135 }