| 1 |
package org.apache.commons.jcs3.engine.behavior; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.EOFException; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStream; |
| 25 |
import java.io.OutputStream; |
| 26 |
import java.nio.ByteBuffer; |
| 27 |
import java.nio.channels.AsynchronousByteChannel; |
| 28 |
import java.nio.channels.ReadableByteChannel; |
| 29 |
import java.nio.channels.WritableByteChannel; |
| 30 |
import java.util.concurrent.ExecutionException; |
| 31 |
import java.util.concurrent.Future; |
| 32 |
import java.util.concurrent.TimeUnit; |
| 33 |
import java.util.concurrent.TimeoutException; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public interface IElementSerializer |
| 40 |
{ |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
<T> byte[] serialize( T obj ) |
| 50 |
throws IOException; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
<T> T deSerialize( byte[] bytes, ClassLoader loader ) |
| 62 |
throws IOException, ClassNotFoundException; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
default <T> int serializeTo(T obj, OutputStream os) |
| 76 |
throws IOException |
| 77 |
{ |
| 78 |
final byte[] serialized = serialize(obj); |
| 79 |
final ByteBuffer buffer = ByteBuffer.allocate(4 + serialized.length); |
| 80 |
buffer.putInt(serialized.length); |
| 81 |
buffer.put(serialized); |
| 82 |
buffer.flip(); |
| 83 |
|
| 84 |
os.write(buffer.array()); |
| 85 |
return buffer.capacity(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
default <T> int serializeTo(T obj, WritableByteChannel oc) |
| 100 |
throws IOException |
| 101 |
{ |
| 102 |
final byte[] serialized = serialize(obj); |
| 103 |
final ByteBuffer buffer = ByteBuffer.allocate(4 + serialized.length); |
| 104 |
buffer.putInt(serialized.length); |
| 105 |
buffer.put(serialized); |
| 106 |
buffer.flip(); |
| 107 |
|
| 108 |
int count = 0; |
| 109 |
while (buffer.hasRemaining()) |
| 110 |
{ |
| 111 |
count += oc.write(buffer); |
| 112 |
} |
| 113 |
return count; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
default <T> int serializeTo(T obj, AsynchronousByteChannel oc, int writeTimeoutMs) |
| 130 |
throws IOException |
| 131 |
{ |
| 132 |
final byte[] serialized = serialize(obj); |
| 133 |
final ByteBuffer buffer = ByteBuffer.allocate(4 + serialized.length); |
| 134 |
buffer.putInt(serialized.length); |
| 135 |
buffer.put(serialized); |
| 136 |
buffer.flip(); |
| 137 |
|
| 138 |
int count = 0; |
| 139 |
while (buffer.hasRemaining()) |
| 140 |
{ |
| 141 |
Future<Integer> bytesWritten = oc.write(buffer); |
| 142 |
try |
| 143 |
{ |
| 144 |
count += bytesWritten.get(writeTimeoutMs, TimeUnit.MILLISECONDS); |
| 145 |
} |
| 146 |
catch (InterruptedException | ExecutionException | TimeoutException e) |
| 147 |
{ |
| 148 |
throw new IOException("Write timeout exceeded " + writeTimeoutMs, e); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return count; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
default <T> T deSerializeFrom(InputStream is, ClassLoader loader) |
| 168 |
throws IOException, ClassNotFoundException |
| 169 |
{ |
| 170 |
final byte[] bufferSize = new byte[4]; |
| 171 |
int read = is.read(bufferSize); |
| 172 |
if (read < 0) |
| 173 |
{ |
| 174 |
throw new EOFException("End of stream reached"); |
| 175 |
} |
| 176 |
assert read == bufferSize.length; |
| 177 |
ByteBuffer size = ByteBuffer.wrap(bufferSize); |
| 178 |
|
| 179 |
byte[] serialized = new byte[size.getInt()]; |
| 180 |
read = is.read(serialized); |
| 181 |
assert read == serialized.length; |
| 182 |
|
| 183 |
return deSerialize(serialized, loader); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
default <T> T deSerializeFrom(ReadableByteChannel ic, ClassLoader loader) |
| 199 |
throws IOException, ClassNotFoundException |
| 200 |
{ |
| 201 |
final ByteBuffer bufferSize = ByteBuffer.allocate(4); |
| 202 |
int read = ic.read(bufferSize); |
| 203 |
if (read < 0) |
| 204 |
{ |
| 205 |
throw new EOFException("End of stream reached (length)"); |
| 206 |
} |
| 207 |
assert read == bufferSize.capacity(); |
| 208 |
bufferSize.flip(); |
| 209 |
|
| 210 |
final ByteBuffer serialized = ByteBuffer.allocate(bufferSize.getInt()); |
| 211 |
while (serialized.remaining() > 0) |
| 212 |
{ |
| 213 |
read = ic.read(serialized); |
| 214 |
if (read < 0) |
| 215 |
{ |
| 216 |
throw new EOFException("End of stream reached (object)"); |
| 217 |
} |
| 218 |
} |
| 219 |
serialized.flip(); |
| 220 |
|
| 221 |
return deSerialize(serialized.array(), loader); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
default <T> T deSerializeFrom(AsynchronousByteChannel ic, int readTimeoutMs, ClassLoader loader) |
| 239 |
throws IOException, ClassNotFoundException |
| 240 |
{ |
| 241 |
final ByteBuffer bufferSize = ByteBuffer.allocate(4); |
| 242 |
Future<Integer> readFuture = ic.read(bufferSize); |
| 243 |
|
| 244 |
try |
| 245 |
{ |
| 246 |
int read = readFuture.get(readTimeoutMs, TimeUnit.MILLISECONDS); |
| 247 |
if (read < 0) |
| 248 |
{ |
| 249 |
throw new EOFException("End of stream reached (length)"); |
| 250 |
} |
| 251 |
assert read == bufferSize.capacity(); |
| 252 |
} |
| 253 |
catch (InterruptedException | ExecutionException | TimeoutException e) |
| 254 |
{ |
| 255 |
throw new IOException("Read timeout exceeded (length)" + readTimeoutMs, e); |
| 256 |
} |
| 257 |
|
| 258 |
bufferSize.flip(); |
| 259 |
|
| 260 |
final ByteBuffer serialized = ByteBuffer.allocate(bufferSize.getInt()); |
| 261 |
while (serialized.remaining() > 0) |
| 262 |
{ |
| 263 |
readFuture = ic.read(serialized); |
| 264 |
try |
| 265 |
{ |
| 266 |
int read = readFuture.get(readTimeoutMs, TimeUnit.MILLISECONDS); |
| 267 |
if (read < 0) |
| 268 |
{ |
| 269 |
throw new EOFException("End of stream reached (object)"); |
| 270 |
} |
| 271 |
} |
| 272 |
catch (InterruptedException | ExecutionException | TimeoutException e) |
| 273 |
{ |
| 274 |
throw new IOException("Read timeout exceeded (object)" + readTimeoutMs, e); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
serialized.flip(); |
| 279 |
|
| 280 |
return deSerialize(serialized.array(), loader); |
| 281 |
} |
| 282 |
} |