KickJava   Java API By Example, From Geeks To Geeks.

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


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.ScatteringByteChannel JavaDoc;
9
10 /**
11  * dev-zero implementation of a readable channel.
12  */

13 public class MockScatteringByteChannel extends MockReadableByteChannel implements ScatteringByteChannel JavaDoc {
14
15   public long read(ByteBuffer JavaDoc[] dsts, int offset, int length) throws IOException JavaDoc {
16     throw new IOException JavaDoc("Not yet implemented");
17   }
18
19   public long read(ByteBuffer JavaDoc[] dsts) throws IOException JavaDoc {
20     checkOpen();
21     if (dsts == null) { throw new IOException JavaDoc("null ByteBuffer[] passed in to read(ByteBuffer[])"); }
22     checkNull(dsts);
23     long bytesRead = 0;
24     for (int pos = 0; pos < dsts.length && bytesRead < getMaxReadCount(); ++pos) {
25       ByteBuffer JavaDoc buffer = dsts[pos];
26       while (buffer.hasRemaining() && bytesRead < getMaxReadCount()) {
27         buffer.put((byte) 0x00);
28         ++bytesRead;
29       }
30     }
31     return bytesRead;
32   }
33
34   private void checkNull(ByteBuffer JavaDoc[] srcs) throws IOException JavaDoc {
35     for (int pos = 0; pos < srcs.length; ++pos) {
36       if (srcs[pos] == null) { throw new IOException JavaDoc("Null ByteBuffer at array position[" + pos + "]"); }
37     }
38   }
39 }
40
Popular Tags