Debian Codegraph @

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

raw
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.qpid.proton.engine.impl;
21
22 import java.nio.ByteBuffer;
23
24 import org.apache.qpid.proton.engine.Transport;
25 import org.apache.qpid.proton.engine.TransportException;
26
27 public class ByteBufferUtils
28 {
29 /**
30 * @return number of bytes poured
31 */
32 public static int pour(ByteBuffer source, ByteBuffer destination)
33 {
34 int numberOfBytesToPour = Math.min(source.remaining(), destination.remaining());
35 ByteBuffer sourceSubBuffer = source.duplicate();
36 sourceSubBuffer.limit(sourceSubBuffer.position() + numberOfBytesToPour);
37 destination.put(sourceSubBuffer);
38 source.position(source.position() + numberOfBytesToPour);
39 return numberOfBytesToPour;
40 }
41
42 /**
43 * Assumes {@code destination} is ready to be written.
44 *
45 * @return number of bytes poured which may be fewer than {@code sizeRequested} if
46 * {@code destination} has insufficient remaining
47 */
48 public static int pourArrayToBuffer(byte[] source, int offset, int sizeRequested, ByteBuffer destination)
49 {
50 int numberToWrite = Math.min(destination.remaining(), sizeRequested);
51 destination.put(source, offset, numberToWrite);
52 return numberToWrite;
53 }
54
55 /**
56 * Pours the contents of {@code source} into {@code destinationTransportInput}, calling
57 * the TransportInput many times if necessary. If the TransportInput returns a {@link org.apache.qpid.proton.engine.TransportResult}
58 * other than ok, data may remain in source.
59 */
60 public static int pourAll(ByteBuffer source, TransportInput destinationTransportInput) throws TransportException
61 {
62 int capacity = destinationTransportInput.capacity();
63 if (capacity == Transport.END_OF_STREAM)
64 {
65 if (source.hasRemaining()) {
66 throw new IllegalStateException("Destination has reached end of stream: " +
67 destinationTransportInput);
68 } else {
69 return Transport.END_OF_STREAM;
70 }
71 }
72
73 int total = source.remaining();
74
75 while(source.hasRemaining() && destinationTransportInput.capacity() > 0)
76 {
77 pour(source, destinationTransportInput.tail());
78 destinationTransportInput.process();
79 }
80
81 return total - source.remaining();
82 }
83
84 /**
85 * Assumes {@code source} is ready to be read.
86 *
87 * @return number of bytes poured which may be fewer than {@code sizeRequested} if
88 * {@code source} has insufficient remaining
89 */
90 public static int pourBufferToArray(ByteBuffer source, byte[] destination, int offset, int sizeRequested)
91 {
92 int numberToRead = Math.min(source.remaining(), sizeRequested);
93 source.get(destination, offset, numberToRead);
94 return numberToRead;
95 }
96
97 public static ByteBuffer newWriteableBuffer(int capacity)
98 {
99 ByteBuffer newBuffer = ByteBuffer.allocate(capacity);
100 return newBuffer;
101 }
102
103 public static ByteBuffer newReadableBuffer(int capacity)
104 {
105 ByteBuffer newBuffer = ByteBuffer.allocate(capacity);
106 newBuffer.flip();
107 return newBuffer;
108 }
109
110 }