01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util.io;
05:
06: import java.io.IOException;
07: import java.nio.ByteBuffer;
08: import java.nio.channels.ReadableByteChannel;
09:
10: /**
11: * dev-zero implementation of a readable byte channel, you can specify the maximum number of bytes to read at once by
12: * calling {@link MockReadableByteChannel#setMaxReadCount(long)}.
13: */
14: public class MockReadableByteChannel extends MockChannel implements
15: ReadableByteChannel {
16:
17: private long maxReadCount = Long.MAX_VALUE;
18:
19: public final synchronized int read(ByteBuffer dst)
20: throws IOException {
21: checkOpen();
22: dst.isReadOnly(); // NPE check
23: int readCount = 0;
24: while (dst.hasRemaining() && readCount < getMaxReadCount()) {
25: dst.put((byte) 0x00);
26: ++readCount;
27: }
28: return readCount;
29: }
30:
31: synchronized final void setMaxReadCount(long maxBytesToReadAtOnce) {
32: maxReadCount = maxBytesToReadAtOnce;
33: }
34:
35: protected final synchronized long getMaxReadCount() {
36: return maxReadCount;
37: }
38:
39: }
|