KickJava   Java API By Example, From Geeks To Geeks.

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


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.WritableByteChannel JavaDoc;
9
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 WritableByteChannel JavaDoc {
15
16   private long maxWriteCount = Long.MAX_VALUE;
17
18   public final synchronized int write(ByteBuffer JavaDoc src) throws IOException JavaDoc {
19     checkOpen();
20     if (src == null) { throw new IOException JavaDoc("null ByteBuffer passed in to write(ByteBuffer)"); }
21     int writeCount = 0;
22     while (src.hasRemaining() && writeCount < getMaxWriteCount()) {
23       src.get();
24       ++writeCount;
25     }
26     return writeCount;
27   }
28
29   synchronized final void setMaxWriteCount(long maxBytesToWriteAtOnce) {
30     maxWriteCount = maxBytesToWriteAtOnce;
31   }
32
33   protected final synchronized long getMaxWriteCount() {
34     return maxWriteCount;
35   }
36
37 }
38
Popular Tags