| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.codec; |
| 22 |
|
| 23 |
import java.nio.ByteBuffer; |
| 24 |
import java.nio.charset.StandardCharsets; |
| 25 |
|
| 26 |
public class CompositeWritableBuffer implements WritableBuffer |
| 27 |
{ |
| 28 |
private final WritableBuffer _first; |
| 29 |
private final WritableBuffer _second; |
| 30 |
|
| 31 |
public CompositeWritableBuffer(WritableBuffer first, WritableBuffer second) |
| 32 |
{ |
| 33 |
_first = first; |
| 34 |
_second = second; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void put(byte b) |
| 39 |
{ |
| 40 |
(_first.hasRemaining() ? _first : _second).put(b); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void putFloat(float f) |
| 45 |
{ |
| 46 |
putInt(Float.floatToRawIntBits(f)); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void putDouble(double d) |
| 51 |
{ |
| 52 |
putLong(Double.doubleToRawLongBits(d)); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void putShort(short s) |
| 57 |
{ |
| 58 |
int remaining = _first.remaining(); |
| 59 |
if(remaining >= 2) |
| 60 |
{ |
| 61 |
_first.putShort(s); |
| 62 |
} |
| 63 |
else if(remaining ==0 ) |
| 64 |
{ |
| 65 |
_second.putShort(s); |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
ByteBuffer wrap = ByteBuffer.wrap(new byte[2]); |
| 70 |
wrap.putShort(s); |
| 71 |
wrap.flip(); |
| 72 |
put(wrap); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void putInt(int i) |
| 78 |
{ |
| 79 |
int remaining = _first.remaining(); |
| 80 |
if(remaining >= 4) |
| 81 |
{ |
| 82 |
_first.putInt(i); |
| 83 |
} |
| 84 |
else if(remaining ==0 ) |
| 85 |
{ |
| 86 |
_second.putInt(i); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
ByteBuffer wrap = ByteBuffer.wrap(new byte[4]); |
| 91 |
wrap.putInt(i); |
| 92 |
wrap.flip(); |
| 93 |
put(wrap); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void putLong(long l) |
| 99 |
{ |
| 100 |
int remaining = _first.remaining(); |
| 101 |
if(remaining >= 8) |
| 102 |
{ |
| 103 |
_first.putLong(l); |
| 104 |
} |
| 105 |
else if(remaining ==0 ) |
| 106 |
{ |
| 107 |
_second.putLong(l); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
ByteBuffer wrap = ByteBuffer.wrap(new byte[8]); |
| 112 |
wrap.putLong(l); |
| 113 |
wrap.flip(); |
| 114 |
put(wrap); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean hasRemaining() |
| 120 |
{ |
| 121 |
return _first.hasRemaining() || _second.hasRemaining(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public int remaining() |
| 126 |
{ |
| 127 |
return _first.remaining()+_second.remaining(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public int position() |
| 132 |
{ |
| 133 |
return _first.position()+_second.position(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public int limit() |
| 138 |
{ |
| 139 |
return _first.limit() + _second.limit(); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void position(int position) |
| 144 |
{ |
| 145 |
int first_limit = _first.limit(); |
| 146 |
if( position <= first_limit ) |
| 147 |
{ |
| 148 |
_first.position(position); |
| 149 |
_second.position(0); |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
_first.position(first_limit); |
| 154 |
_second.position(position - first_limit); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public void put(byte[] src, int offset, int length) |
| 160 |
{ |
| 161 |
final int firstRemaining = _first.remaining(); |
| 162 |
if(firstRemaining > 0) |
| 163 |
{ |
| 164 |
if(firstRemaining >= length) |
| 165 |
{ |
| 166 |
_first.put(src, offset, length); |
| 167 |
return; |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
_first.put(src,offset, firstRemaining); |
| 172 |
} |
| 173 |
} |
| 174 |
_second.put(src, offset+firstRemaining, length-firstRemaining); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void put(ByteBuffer payload) |
| 179 |
{ |
| 180 |
int firstRemaining = _first.remaining(); |
| 181 |
if(firstRemaining > 0) |
| 182 |
{ |
| 183 |
if(firstRemaining >= payload.remaining()) |
| 184 |
{ |
| 185 |
_first.put(payload); |
| 186 |
return; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
int limit = payload.limit(); |
| 191 |
payload.limit(payload.position()+firstRemaining); |
| 192 |
_first.put(payload); |
| 193 |
payload.limit(limit); |
| 194 |
} |
| 195 |
} |
| 196 |
_second.put(payload); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public String toString() |
| 201 |
{ |
| 202 |
return _first.toString() + " + "+_second.toString(); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void put(ReadableBuffer payload) { |
| 207 |
int firstRemaining = _first.remaining(); |
| 208 |
if(firstRemaining > 0) |
| 209 |
{ |
| 210 |
if(firstRemaining >= payload.remaining()) |
| 211 |
{ |
| 212 |
_first.put(payload); |
| 213 |
return; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
int limit = payload.limit(); |
| 218 |
payload.limit(payload.position()+firstRemaining); |
| 219 |
_first.put(payload); |
| 220 |
payload.limit(limit); |
| 221 |
} |
| 222 |
} |
| 223 |
_second.put(payload); |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void put(String value) |
| 228 |
{ |
| 229 |
if (_first.hasRemaining()) |
| 230 |
{ |
| 231 |
byte[] utf8Bytes = value.getBytes(StandardCharsets.UTF_8); |
| 232 |
put(utf8Bytes, 0, utf8Bytes.length); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
_second.put(value); |
| 237 |
} |
| 238 |
} |
| 239 |
} |