KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > io > MockReadableByteChannel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.io;
5
6 import java.io.IOException JavaDoc;
7 import java.nio.ByteBuffer JavaDoc;
8 import java.nio.channels.ReadableByteChannel JavaDoc;
9
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 ReadableByteChannel JavaDoc {
15
16   private long maxReadCount = Long.MAX_VALUE;
17
18   public final synchronized int read(ByteBuffer JavaDoc dst) throws IOException JavaDoc {
19     checkOpen();
20     dst.isReadOnly(); // NPE check
21
int readCount = 0;
22     while (dst.hasRemaining() && readCount < getMaxReadCount()) {
23       dst.put((byte) 0x00);
24       ++readCount;
25     }
26     return readCount;
27   }
28
29   synchronized final void setMaxReadCount(long maxBytesToReadAtOnce) {
30     maxReadCount = maxBytesToReadAtOnce;
31   }
32
33   protected final synchronized long getMaxReadCount() {
34     return maxReadCount;
35   }
36
37 }
38
Popular Tags