01: /*
02: * @(#)CharBufferLineReader.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.nio;
10:
11: import java.io.*;
12: import java.nio.*;
13: import pnuts.lang.*;
14: import pnuts.lang.Package;
15: import org.pnuts.text.*;
16:
17: class CharBufferLineReader extends AbstractLineReader {
18:
19: protected CharBuffer cbuf;
20: protected LineHandler handler;
21:
22: final static int defaultBufferSize = 8192;
23:
24: /**
25: * Constructor
26: *
27: * @param cbuf A CharBuffer
28: * @param sz Buffer size
29: *
30: * @exception IllegalArgumentException If sz is <= 0
31: */
32: public CharBufferLineReader(CharBuffer cbuf, int sz) {
33: super (sz);
34: this .cbuf = cbuf;
35: }
36:
37: /**
38: * Constructor
39: *
40: * @param cbuf A CharBuffer
41: */
42: public CharBufferLineReader(CharBuffer cbuf) {
43: this (cbuf, defaultBufferSize);
44: }
45:
46: /**
47: * Constructor
48: */
49: protected CharBufferLineReader() {
50: }
51:
52: protected int fill(char[] c, int offset, int size)
53: throws IOException {
54: int rem = cbuf.remaining();
55: if (rem <= 0) {
56: return -1;
57: }
58: if (size > rem) {
59: size = rem;
60: }
61: cbuf.get(c, offset, size);
62: return size;
63: }
64:
65: public void setLineHandler(LineHandler handler) {
66: this .handler = handler;
67: }
68:
69: public LineHandler getLineHandler() {
70: return this .handler;
71: }
72:
73: protected void process(char[] c, int offset, int length) {
74: handler.process(c, offset, length);
75: }
76: }
|