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.GatheringByteChannel;
09:
10: /**
11: * dev-null implementation of a gathering byte channel.
12: */
13: public class MockGatheringByteChannel extends MockWritableByteChannel
14: implements GatheringByteChannel {
15:
16: public synchronized long write(ByteBuffer[] srcs, int offset,
17: int length) throws IOException {
18: checkOpen();
19: if (srcs == null) {
20: throw new IOException(
21: "null ByteBuffer[] passed in to write(ByteBuffer[], int, int)");
22: }
23: checkNull(srcs);
24: throw new IOException("Not yet implemented");
25: }
26:
27: public synchronized long write(ByteBuffer[] srcs)
28: throws IOException {
29: checkOpen();
30: if (srcs == null) {
31: throw new IOException(
32: "null ByteBuffer[] passed in to write(ByteBuffer[])");
33: }
34: checkNull(srcs);
35: long bytesWritten = 0;
36: for (int pos = 0; pos < srcs.length
37: && bytesWritten < getMaxWriteCount(); ++pos) {
38: ByteBuffer buffer = srcs[pos];
39: while (buffer.hasRemaining()
40: && bytesWritten < getMaxWriteCount()) {
41: buffer.get();
42: ++bytesWritten;
43: }
44: }
45: return bytesWritten;
46: }
47:
48: private void checkNull(ByteBuffer[] srcs) throws IOException {
49: for (int pos = 0; pos < srcs.length; ++pos) {
50: if (srcs[pos] == null) {
51: throw new IOException(
52: "Null ByteBuffer at array position[" + pos
53: + "]");
54: }
55: }
56: }
57: }
|