KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > ByteishWrapper


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;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.exception.TCRuntimeException;
8
9 import java.nio.ByteBuffer JavaDoc;
10
11 /**
12  * Only wanted to write the real dump() implementation once, so this wrapper should mask whether we are working with an
13  * NIO byte buffer or a TC byte buffer.
14  */

15 class ByteishWrapper implements ByteishBuffer {
16
17   private ByteBuffer nioBuf;
18   private TCByteBuffer tcBuf;
19
20   private ByteishWrapper() {
21     throw new TCRuntimeException("Private constructor should not be called");
22   }
23
24   ByteishWrapper(ByteBuffer nioBuf, TCByteBuffer tcBuf) {
25     if (nioBuf != null) {
26       this.nioBuf = nioBuf;
27     } else if (tcBuf != null) {
28       this.tcBuf = tcBuf;
29     } else {
30       throw new TCRuntimeException("Must specify a ByteBuffer or TCByteBuffer");
31     }
32   }
33
34   public byte get(int position) {
35     return nioBuf != null ? nioBuf.get(position) : tcBuf.get(position);
36   }
37
38   public int limit() {
39     return nioBuf != null ? nioBuf.limit() : tcBuf.limit();
40   }
41
42 }
Popular Tags