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.WritableByteChannel;
09:
10: /**
11: * dev-null implementation of a writable byte channel, you can specify the maximum number of bytes to write at once by
12: * calling {@link MockWritableByteChannel#setMaxWriteCount(long)}.
13: */
14: public class MockWritableByteChannel extends MockChannel implements
15: WritableByteChannel {
16:
17: private long maxWriteCount = Long.MAX_VALUE;
18:
19: public final synchronized int write(ByteBuffer src)
20: throws IOException {
21: checkOpen();
22: if (src == null) {
23: throw new IOException(
24: "null ByteBuffer passed in to write(ByteBuffer)");
25: }
26: int writeCount = 0;
27: while (src.hasRemaining() && writeCount < getMaxWriteCount()) {
28: src.get();
29: ++writeCount;
30: }
31: return writeCount;
32: }
33:
34: synchronized final void setMaxWriteCount(long maxBytesToWriteAtOnce) {
35: maxWriteCount = maxBytesToWriteAtOnce;
36: }
37:
38: protected final synchronized long getMaxWriteCount() {
39: return maxWriteCount;
40: }
41:
42: }
|