|
14 | 14 | import net.lecousin.framework.core.test.LCCoreAbstractTest;
|
15 | 15 | import net.lecousin.framework.io.IO;
|
16 | 16 | import net.lecousin.framework.io.IOUtil;
|
| 17 | +import net.lecousin.framework.io.data.ByteArray; |
17 | 18 | import net.lecousin.framework.mutable.MutableBoolean;
|
18 | 19 | import net.lecousin.framework.util.ConcurrentCloseable;
|
19 | 20 | import net.lecousin.framework.util.Pair;
|
@@ -189,6 +190,155 @@ public AsyncSupplier<Long, IOException> getSizeAsync() {
|
189 | 190 |
|
190 | 191 | }
|
191 | 192 |
|
| 193 | + /** Return an IOException after first bytes. */ |
| 194 | + public static class ReadableErrorAfterBeginning extends ConcurrentCloseable<IOException> implements IO.Readable, IO.Readable.Buffered { |
| 195 | + |
| 196 | + public ReadableErrorAfterBeginning(ByteArray beginning) { |
| 197 | + this.beginning = beginning; |
| 198 | + } |
| 199 | + |
| 200 | + protected IOException error = new IOException("it's normal"); |
| 201 | + protected ByteArray beginning; |
| 202 | + |
| 203 | + @Override |
| 204 | + public String getSourceDescription() { |
| 205 | + return getClass().getSimpleName(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public IO getWrappedIO() { |
| 210 | + return null; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void setPriority(byte priority) { |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public TaskManager getTaskManager() { |
| 219 | + return Threading.getCPUTaskManager(); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public int read() throws IOException { |
| 224 | + if (!beginning.hasRemaining()) |
| 225 | + throw error; |
| 226 | + return beginning.get() & 0xFF; |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public int read(byte[] buffer, int offset, int len) throws IOException { |
| 231 | + if (!beginning.hasRemaining()) |
| 232 | + throw error; |
| 233 | + int l = Math.min(len, beginning.remaining()); |
| 234 | + beginning.get(buffer, offset, l); |
| 235 | + return l; |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public int readFully(byte[] buffer) throws IOException { |
| 240 | + return read(buffer, 0, buffer.length); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public int skip(int skip) throws IOException { |
| 245 | + if (!beginning.hasRemaining()) |
| 246 | + throw error; |
| 247 | + int l = Math.min(skip, beginning.remaining()); |
| 248 | + beginning.moveForward(l); |
| 249 | + return l; |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public AsyncSupplier<ByteBuffer, IOException> readNextBufferAsync(Consumer<Pair<ByteBuffer, IOException>> ondone) { |
| 254 | + if (!beginning.hasRemaining()) |
| 255 | + return IOUtil.error(error, ondone); |
| 256 | + ByteBuffer b = beginning.toByteBuffer(); |
| 257 | + beginning.goToEnd(); |
| 258 | + return IOUtil.success(b, ondone); |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public ByteBuffer readNextBuffer() throws IOException { |
| 263 | + if (!beginning.hasRemaining()) |
| 264 | + throw error; |
| 265 | + ByteBuffer b = beginning.toByteBuffer(); |
| 266 | + beginning.goToEnd(); |
| 267 | + return b; |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public int readAsync() throws IOException { |
| 272 | + return read(); |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public AsyncSupplier<Integer, IOException> readFullySyncIfPossible(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) { |
| 277 | + if (!beginning.hasRemaining()) |
| 278 | + return IOUtil.error(error, ondone); |
| 279 | + int l = Math.min(beginning.remaining(), buffer.remaining()); |
| 280 | + buffer.put(beginning.getArray(), beginning.getCurrentArrayOffset(), l); |
| 281 | + beginning.moveForward(l); |
| 282 | + return IOUtil.success(Integer.valueOf(l), ondone); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public IAsync<IOException> canStartReading() { |
| 287 | + return new Async<>(true); |
| 288 | + } |
| 289 | + |
| 290 | + @Override |
| 291 | + public int readSync(ByteBuffer buffer) throws IOException { |
| 292 | + if (!beginning.hasRemaining()) |
| 293 | + throw error; |
| 294 | + int l = Math.min(beginning.remaining(), buffer.remaining()); |
| 295 | + buffer.put(beginning.getArray(), beginning.getCurrentArrayOffset(), l); |
| 296 | + beginning.moveForward(l); |
| 297 | + return l; |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public AsyncSupplier<Integer, IOException> readAsync(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) { |
| 302 | + return readFullySyncIfPossible(buffer, ondone); |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public int readFullySync(ByteBuffer buffer) throws IOException { |
| 307 | + return readSync(buffer); |
| 308 | + } |
| 309 | + |
| 310 | + @Override |
| 311 | + public AsyncSupplier<Integer, IOException> readFullyAsync(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) { |
| 312 | + return readAsync(buffer, ondone); |
| 313 | + } |
| 314 | + |
| 315 | + @Override |
| 316 | + public long skipSync(long n) throws IOException { |
| 317 | + return skip((int)n); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public AsyncSupplier<Long, IOException> skipAsync(long n, Consumer<Pair<Long, IOException>> ondone) { |
| 322 | + return IOUtil.skipAsyncUsingSync(this, n, ondone); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public byte getPriority() { |
| 327 | + return Task.PRIORITY_NORMAL; |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + protected IAsync<IOException> closeUnderlyingResources() { |
| 332 | + return new Async<>(true); |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + protected void closeResources(Async<IOException> ondone) { |
| 337 | + ondone.unblock(); |
| 338 | + } |
| 339 | + |
| 340 | + } |
| 341 | + |
192 | 342 | protected abstract IO.Readable getReadable(ReadableAlwaysError io) throws Exception;
|
193 | 343 |
|
194 | 344 | protected abstract IO.Readable.Buffered getReadableBuffered(ReadableAlwaysError io) throws Exception;
|
|
0 commit comments