| 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 |
|
| 22 |
package org.apache.qpid.proton.codec; |
| 23 |
|
| 24 |
import java.nio.BufferOverflowException; |
| 25 |
import java.nio.ByteBuffer; |
| 26 |
|
| 27 |
public interface WritableBuffer { |
| 28 |
void put(byte b); |
| 29 |
|
| 30 |
void putFloat(float f); |
| 31 |
|
| 32 |
void putDouble(double d); |
| 33 |
|
| 34 |
void put(byte[] src, int offset, int length); |
| 35 |
|
| 36 |
void putShort(short s); |
| 37 |
|
| 38 |
void putInt(int i); |
| 39 |
|
| 40 |
void putLong(long l); |
| 41 |
|
| 42 |
boolean hasRemaining(); |
| 43 |
|
| 44 |
default void ensureRemaining(int requiredRemaining) { |
| 45 |
// No-op to allow for drop in updates |
| 46 |
} |
| 47 |
|
| 48 |
int remaining(); |
| 49 |
|
| 50 |
int position(); |
| 51 |
|
| 52 |
void position(int position); |
| 53 |
|
| 54 |
void put(ByteBuffer payload); |
| 55 |
|
| 56 |
void put(ReadableBuffer payload); |
| 57 |
|
| 58 |
default void put(final String value) { |
| 59 |
final int length = value.length(); |
| 60 |
|
| 61 |
for (int i = 0; i < length; i++) { |
| 62 |
int c = value.charAt(i); |
| 63 |
if ((c & 0xFF80) == 0) { |
| 64 |
// U+0000..U+007F |
| 65 |
put((byte) c); |
| 66 |
} else if ((c & 0xF800) == 0) { |
| 67 |
// U+0080..U+07FF |
| 68 |
put((byte) (0xC0 | ((c >> 6) & 0x1F))); |
| 69 |
put((byte) (0x80 | (c & 0x3F))); |
| 70 |
} else if ((c & 0xD800) != 0xD800 || (c > 0xDBFF)) { |
| 71 |
// U+0800..U+FFFF - excluding surrogate pairs |
| 72 |
put((byte) (0xE0 | ((c >> 12) & 0x0F))); |
| 73 |
put((byte) (0x80 | ((c >> 6) & 0x3F))); |
| 74 |
put((byte) (0x80 | (c & 0x3F))); |
| 75 |
} else { |
| 76 |
int low; |
| 77 |
|
| 78 |
if ((++i == length) || ((low = value.charAt(i)) & 0xDC00) != 0xDC00) { |
| 79 |
throw new IllegalArgumentException("String contains invalid Unicode code points"); |
| 80 |
} |
| 81 |
|
| 82 |
c = 0x010000 + ((c & 0x03FF) << 10) + (low & 0x03FF); |
| 83 |
|
| 84 |
put((byte) (0xF0 | ((c >> 18) & 0x07))); |
| 85 |
put((byte) (0x80 | ((c >> 12) & 0x3F))); |
| 86 |
put((byte) (0x80 | ((c >> 6) & 0x3F))); |
| 87 |
put((byte) (0x80 | (c & 0x3F))); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
int limit(); |
| 93 |
|
| 94 |
class ByteBufferWrapper implements WritableBuffer { |
| 95 |
private final ByteBuffer _buf; |
| 96 |
|
| 97 |
public ByteBufferWrapper(ByteBuffer buf) { |
| 98 |
_buf = buf; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void put(byte b) { |
| 103 |
_buf.put(b); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void putFloat(float f) { |
| 108 |
_buf.putFloat(f); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void putDouble(double d) { |
| 113 |
_buf.putDouble(d); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void put(byte[] src, int offset, int length) { |
| 118 |
_buf.put(src, offset, length); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void putShort(short s) { |
| 123 |
_buf.putShort(s); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void putInt(int i) { |
| 128 |
_buf.putInt(i); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void putLong(long l) { |
| 133 |
_buf.putLong(l); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean hasRemaining() { |
| 138 |
return _buf.hasRemaining(); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void ensureRemaining(int remaining) { |
| 143 |
if (remaining < 0) { |
| 144 |
throw new IllegalArgumentException("Required remaining bytes cannot be negative"); |
| 145 |
} |
| 146 |
|
| 147 |
if (_buf.remaining() < remaining) { |
| 148 |
IndexOutOfBoundsException cause = new IndexOutOfBoundsException(String.format( |
| 149 |
"Requested min remaining bytes(%d) exceeds remaining(%d) in underlying ByteBuffer: %s", |
| 150 |
remaining, _buf.remaining(), _buf)); |
| 151 |
|
| 152 |
throw (BufferOverflowException) new BufferOverflowException().initCause(cause); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public int remaining() { |
| 158 |
return _buf.remaining(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public int position() { |
| 163 |
return _buf.position(); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void position(int position) { |
| 168 |
_buf.position(position); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void put(ByteBuffer src) { |
| 173 |
_buf.put(src); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void put(ReadableBuffer src) { |
| 178 |
src.get(this); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void put(final String value) { |
| 183 |
final int length = value.length(); |
| 184 |
|
| 185 |
int pos = _buf.position(); |
| 186 |
|
| 187 |
for (int i = 0; i < length; i++) { |
| 188 |
int c = value.charAt(i); |
| 189 |
try { |
| 190 |
if ((c & 0xFF80) == 0) { |
| 191 |
// U+0000..U+007F |
| 192 |
put(pos++, (byte) c); |
| 193 |
} else if ((c & 0xF800) == 0) { |
| 194 |
// U+0080..U+07FF |
| 195 |
put(pos++, (byte) (0xC0 | ((c >> 6) & 0x1F))); |
| 196 |
put(pos++, (byte) (0x80 | (c & 0x3F))); |
| 197 |
} else if ((c & 0xD800) != 0xD800 || (c > 0xDBFF)) { |
| 198 |
// U+0800..U+FFFF - excluding surrogate pairs |
| 199 |
put(pos++, (byte) (0xE0 | ((c >> 12) & 0x0F))); |
| 200 |
put(pos++, (byte) (0x80 | ((c >> 6) & 0x3F))); |
| 201 |
put(pos++, (byte) (0x80 | (c & 0x3F))); |
| 202 |
} else { |
| 203 |
int low; |
| 204 |
|
| 205 |
if ((++i == length) || ((low = value.charAt(i)) & 0xDC00) != 0xDC00) { |
| 206 |
throw new IllegalArgumentException("String contains invalid Unicode code points"); |
| 207 |
} |
| 208 |
|
| 209 |
c = 0x010000 + ((c & 0x03FF) << 10) + (low & 0x03FF); |
| 210 |
|
| 211 |
put(pos++, (byte) (0xF0 | ((c >> 18) & 0x07))); |
| 212 |
put(pos++, (byte) (0x80 | ((c >> 12) & 0x3F))); |
| 213 |
put(pos++, (byte) (0x80 | ((c >> 6) & 0x3F))); |
| 214 |
put(pos++, (byte) (0x80 | (c & 0x3F))); |
| 215 |
} |
| 216 |
} |
| 217 |
catch(IndexOutOfBoundsException ioobe) { |
| 218 |
throw new BufferOverflowException(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// Now move the buffer position to reflect the work done here |
| 223 |
_buf.position(pos); |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public int limit() { |
| 228 |
return _buf.limit(); |
| 229 |
} |
| 230 |
|
| 231 |
public ByteBuffer byteBuffer() { |
| 232 |
return _buf; |
| 233 |
} |
| 234 |
|
| 235 |
public ReadableBuffer toReadableBuffer() { |
| 236 |
return ReadableBuffer.ByteBufferReader.wrap((ByteBuffer) _buf.duplicate().flip()); |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public String toString() { |
| 241 |
return String.format("[pos: %d, limit: %d, remaining:%d]", _buf.position(), _buf.limit(), _buf.remaining()); |
| 242 |
} |
| 243 |
|
| 244 |
public static ByteBufferWrapper allocate(int size) { |
| 245 |
ByteBuffer allocated = ByteBuffer.allocate(size); |
| 246 |
return new ByteBufferWrapper(allocated); |
| 247 |
} |
| 248 |
|
| 249 |
public static ByteBufferWrapper wrap(ByteBuffer buffer) { |
| 250 |
return new ByteBufferWrapper(buffer); |
| 251 |
} |
| 252 |
|
| 253 |
public static ByteBufferWrapper wrap(byte[] bytes) { |
| 254 |
return new ByteBufferWrapper(ByteBuffer.wrap(bytes)); |
| 255 |
} |
| 256 |
|
| 257 |
private void put(int index, byte value) { |
| 258 |
if (_buf.hasArray()) { |
| 259 |
_buf.array()[_buf.arrayOffset() + index] = value; |
| 260 |
} else { |
| 261 |
_buf.put(index, value); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |