1 /*
2  * @(#) $Id: JSTKBuffer.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.ssl;
11
12import org.jstk.JSTKArgs;
13import org.jstk.JSTKOptions;
14import java.nio.ByteBuffer;
15
16public abstract class JSTKBuffer {
17    public static class NIOByteBuffer extends JSTKBuffer {
18        private ByteBuffer bb;
19        protected NIOByteBuffer(int bufsize){
20            bb = ByteBuffer.allocateDirect(bufsize);
21        }
22
23        public int length(){
24            return bb.capacity();
25        }
26        public int getNBytes(){
27            return bb.position();
28        }
29        public void setNBytes(int n){
30        }
31        public byte[] getBytes(){
32            bb.flip();
33            byte[] buf = new byte[bb.limit()];
34            bb.get(buf);
35            bb.clear();
36            return buf;
37        }
38        public void putBytes(byte[] buf){
39            bb.put(buf);
40        }
41        public void putBytes(byte[] buf, int off, int len){
42            bb.put(buf, off, len);
43        }
44        public ByteBuffer getByteBuffer(){
45            return bb;
46        }
47        public byte[] getByteArray(){
48            return null;
49        }
50        public void clear(){
51            bb.clear();
52        }
53    }
54
55    public static class OrdByteBuffer extends JSTKBuffer {
56        byte[] buf;
57        int n;
58
59        protected OrdByteBuffer(int bufsize){
60            buf = new byte[bufsize];
61            n = 0;
62        }
63        public int length(){
64            return buf.length;
65        }
66        public int getNBytes(){
67            return n;
68        }
69        public void setNBytes(int n){
70            this.n = n;
71        }
72        public byte[] getBytes(){
73            byte[] tbuf = new byte[n];
74            System.arraycopy(buf, 0, tbuf, 0, n);
75            n = 0;
76            return tbuf;
77        }
78        public void putBytes(byte[] tbuf){
79            System.arraycopy(tbuf, 0, buf, n, tbuf.length);
80            n += tbuf.length;
81        }
82        public void putBytes(byte[] tbuf, int off, int len){
83            System.arraycopy(tbuf, off, buf, n, len);
84            n += len;
85        }
86        public ByteBuffer getByteBuffer(){
87            return null;
88        }
89        public byte[] getByteArray(){
90            return buf;
91        }
92        public void clear(){
93        }
94    }
95
96    public static JSTKBuffer getInstance(int bufsize, JSTKArgs args){
97        boolean nio = Boolean.valueOf(args.get("nio")).booleanValue();
98        if (nio)
99            return new NIOByteBuffer(bufsize);
00        else
01            return new OrdByteBuffer(bufsize);
02    }
03
04    public static JSTKBuffer getInstance(int bufsize){
05        return new NIOByteBuffer(bufsize);
06    }
07
08    public abstract int length();
09    public abstract int getNBytes();
10    public abstract void setNBytes(int n);
11    public abstract byte[] getBytes();
12    public abstract void putBytes(byte[] buf);
13    public abstract void putBytes(byte[] buf, int off, int len);
14    public abstract ByteBuffer getByteBuffer();
15    public abstract byte[] getByteArray();
16    public abstract void clear();
17
18    public static void main(String[] args){
19        JSTKOptions opts = new JSTKOptions();
20        opts.parse(args, 0);
21        byte[] buf;
22        String data = "test data";
23        JSTKBuffer jb = JSTKBuffer.getInstance(1024, opts);
24
25        System.out.println("First Round::");
26        System.out.println("jb.length() = " + jb.length() + ", jb.bytes() = " + jb.getNBytes());
27        jb.putBytes(data.getBytes());
28        System.out.println("jb.length() = " + jb.length() + ", jb.bytes() = " + jb.getNBytes());
29        buf = jb.getBytes();
30        System.out.println("buf = " + new String(buf));
31
32        System.out.println("Second Round::");
33        System.out.println("jb.length() = " + jb.length() + ", jb.bytes() = " + jb.getNBytes());
34        jb.putBytes(data.getBytes());
35        System.out.println("jb.length() = " + jb.length() + ", jb.bytes() = " + jb.getNBytes());
36        buf = jb.getBytes();
37        System.out.println("buf = " + new String(buf));
38    }
39}
40