01: /*
02: * @(#)ByteBufferLineInputStream.java 1.2 04/12/06
03: *
04: * Copyright (c) 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 org.pnuts.text.*;
12: import java.io.*;
13: import java.nio.*;
14:
15: public class ByteBufferLineInputStream extends AbstractLineInputStream {
16:
17: protected ByteBuffer bbuf;
18: protected LineHandler handler;
19: protected boolean needToClose;
20:
21: final static int defaultBufferSize = 8192;
22:
23: public ByteBufferLineInputStream(ByteBuffer bbuf,
24: LineHandler handler, boolean needToClose) {
25: this (bbuf, defaultBufferSize, handler, needToClose);
26: }
27:
28: /**
29: * Constructor
30: *
31: * @param cbuf A ByteBuffer
32: * @param sz Buffer size
33: *
34: * @exception IllegalArgumentException If sz is <= 0
35: */
36: public ByteBufferLineInputStream(ByteBuffer bbuf, int sz,
37: LineHandler handler, boolean needToClose) {
38: super (sz);
39: this .bbuf = bbuf;
40: this .handler = handler;
41: this .needToClose = needToClose;
42: }
43:
44: /**
45: * Constructor
46: */
47: protected ByteBufferLineInputStream() {
48: }
49:
50: protected int fill(byte[] b, int offset, int size)
51: throws IOException {
52: int rem = bbuf.remaining();
53: if (rem <= 0) {
54: return -1;
55: }
56: if (size > rem) {
57: size = rem;
58: }
59: bbuf.get(b, offset, size);
60: return size;
61: }
62:
63: protected void process(byte[] b, int offset, int length) {
64: handler.process(b, offset, length);
65: }
66: }
|