Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/engine/impl/FrameWriterBuffer.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.qpid.proton.engine.impl;
18
19 import java.nio.ByteBuffer;
20
21 import org.apache.qpid.proton.codec.ReadableBuffer;
22 import org.apache.qpid.proton.codec.WritableBuffer;
23
24 public class FrameWriterBuffer implements WritableBuffer {
25
26 public final static int DEFAULT_CAPACITY = 1024;
27
28 byte array[];
29 int position;
30
31 /**
32 * Creates a new WritableBuffer with default capacity.
33 */
34 public FrameWriterBuffer() {
35 this(DEFAULT_CAPACITY);
36 }
37
38 /**
39 * Create a new WritableBuffer with the given capacity.
40 *
41 * @param capacity
42 * the inital capacity to allocate for this buffer.
43 */
44 public FrameWriterBuffer(int capacity) {
45 this.array = new byte[capacity];
46 }
47
48 public byte[] array() {
49 return array;
50 }
51
52 public int arrayOffset() {
53 return 0;
54 }
55
56 @Override
57 public void put(byte b) {
58 ensureRemaining(Byte.BYTES);
59 array[position++] = b;
60 }
61
62 @Override
63 public void putShort(short value) {
64 ensureRemaining(Short.BYTES);
65 array[position++] = (byte)(value >>> 8);
66 array[position++] = (byte)(value >>> 0);
67 }
68
69 @Override
70 public void putInt(int value) {
71 ensureRemaining(Integer.BYTES);
72 array[position++] = (byte)(value >>> 24);
73 array[position++] = (byte)(value >>> 16);
74 array[position++] = (byte)(value >>> 8);
75 array[position++] = (byte)(value >>> 0);
76 }
77
78 @Override
79 public void putLong(long value) {
80 ensureRemaining(Long.BYTES);
81 array[position++] = (byte)(value >>> 56);
82 array[position++] = (byte)(value >>> 48);
83 array[position++] = (byte)(value >>> 40);
84 array[position++] = (byte)(value >>> 32);
85 array[position++] = (byte)(value >>> 24);
86 array[position++] = (byte)(value >>> 16);
87 array[position++] = (byte)(value >>> 8);
88 array[position++] = (byte)(value >>> 0);
89 }
90
91 @Override
92 public void putFloat(float value) {
93 putInt(Float.floatToRawIntBits(value));
94 }
95
96 @Override
97 public void putDouble(double value) {
98 putLong(Double.doubleToRawLongBits(value));
99 }
100
101 @Override
102 public void put(byte[] src, int offset, int length) {
103 if (length == 0) {
104 return;
105 }
106
107 ensureRemaining(length);
108 System.arraycopy(src, offset, array, position, length);
109 position += length;
110 }
111
112 @Override
113 public void put(ByteBuffer payload) {
114 final int toCopy = payload.remaining();
115 ensureRemaining(toCopy);
116
117 if (payload.hasArray()) {
118 System.arraycopy(payload.array(), payload.arrayOffset() + payload.position(), array, position, toCopy);
119 payload.position(payload.position() + toCopy);
120 } else {
121 payload.get(array, position, toCopy);
122 }
123
124 position += toCopy;
125 }
126
127 @Override
128 public void put(ReadableBuffer payload) {
129 final int toCopy = payload.remaining();
130 ensureRemaining(toCopy);
131
132 if (payload.hasArray()) {
133 System.arraycopy(payload.array(), payload.arrayOffset() + payload.position(), array, position, toCopy);
134 payload.position(payload.position() + toCopy);
135 } else {
136 payload.get(array, position, toCopy);
137 }
138
139 position += toCopy;
140 }
141
142 @Override
143 public boolean hasRemaining() {
144 return position < Integer.MAX_VALUE;
145 }
146
147 @Override
148 public int remaining() {
149 return Integer.MAX_VALUE - position;
150 }
151
152 /**
153 * Ensures the the buffer has at least the requiredRemaining space specified.
154 * <p>
155 * The internal buffer will be doubled if the requested capacity is less than that
156 * amount or the buffer will be expanded to the full new requiredRemaining value.
157 *
158 * @param requiredRemaining
159 * the minimum remaining bytes needed to meet the next write operation.
160 */
161 @Override
162 public void ensureRemaining(int requiredRemaining) {
163 if (requiredRemaining > array.length - position) {
164 byte newBuffer[] = new byte[Math.max(array.length << 1, requiredRemaining + position)];
165 System.arraycopy(array, 0, newBuffer, 0, array.length);
166 array = newBuffer;
167 }
168 }
169
170 @Override
171 public int position() {
172 return position;
173 }
174
175 @Override
176 public void position(int position) {
177 if (position < 0) {
178 throw new IllegalArgumentException("Requested new buffer position cannot be negative");
179 }
180
181 if (position > array.length) {
182 ensureRemaining(position - array.length);
183 }
184
185 this.position = position;
186 }
187
188 @Override
189 public int limit() {
190 return Integer.MAX_VALUE;
191 }
192
193 /**
194 * Copy bytes from this buffer into the target buffer and compacts this buffer.
195 * <p>
196 * Copy either all bytes written into this buffer (start to current position) or
197 * as many as will fit if the target capacity is less that the bytes written. Bytes
198 * not read from this buffer are moved to the front of the buffer and the position is
199 * reset to the end of the copied region.
200 *
201 * @param target
202 * The array to move bytes to from those written into this buffer.
203 *
204 * @return the number of bytes transfered to the target buffer.
205 */
206 public int transferTo(ByteBuffer target) {
207 int size = Math.min(position, target.remaining());
208
209 if (size == 0) {
210 return 0;
211 }
212
213 if (target.hasArray()) {
214 System.arraycopy(array, 0, target.array(), target.arrayOffset() + target.position(), size);
215 target.position(target.position() + size);
216 } else {
217 target.put(array, 0, size);
218 }
219
220 // Compact any remaining data to the front of the array so that new writes can reuse
221 // space previously allocated and not extend the array if possible.
222 if (size != position) {
223 int remainder = position - size;
224 System.arraycopy(array, size, array, 0, remainder);
225 position = remainder; // ensure we are at end of unread chunk
226 } else {
227 position = 0; // reset to empty state.
228 }
229
230 return size;
231 }
232 }