001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.knowgate.jcifs.smb;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023:
024: import com.knowgate.debug.*;
025:
026: class TransactNamedPipeInputStream extends InputStream {
027:
028: private static final int INIT_PIPE_SIZE = 4096;
029:
030: private SmbNamedPipe pipe;
031: private byte[] pipe_buf = new byte[INIT_PIPE_SIZE];
032: private int beg_idx, nxt_idx, used;
033:
034: Object lock;
035:
036: TransactNamedPipeInputStream(SmbNamedPipe pipe) {
037: this .pipe = pipe;
038: lock = new Object();
039: }
040:
041: public void close() throws IOException {
042: pipe.close();
043: }
044:
045: public int read() throws IOException {
046: int result = -1;
047:
048: synchronized (lock) {
049: try {
050: while (used == 0) {
051: lock.wait();
052: }
053: } catch (InterruptedException ie) {
054: throw new IOException(ie.getMessage());
055: }
056: result = pipe_buf[beg_idx] & 0xFF;
057: beg_idx = (beg_idx + 1) % pipe_buf.length;
058: }
059: return result;
060: }
061:
062: public int read(byte[] b) throws IOException {
063: return read(b, 0, b.length);
064: }
065:
066: public int read(byte[] b, int off, int len) throws IOException {
067: int result = -1;
068: int i;
069:
070: if (len <= 0) {
071: return 0;
072: }
073: synchronized (lock) {
074: try {
075: while (used == 0) {
076: lock.wait();
077: }
078: } catch (InterruptedException ie) {
079: throw new IOException(ie.getMessage());
080: }
081: i = pipe_buf.length - beg_idx;
082: result = len > used ? used : len;
083: if (used > i && result > i) {
084: System.arraycopy(pipe_buf, beg_idx, b, off, i);
085: off += i;
086: System.arraycopy(pipe_buf, 0, b, off, result - i);
087: } else {
088: System.arraycopy(pipe_buf, beg_idx, b, off, result);
089: }
090: used -= result;
091: beg_idx = (beg_idx + result) % pipe_buf.length;
092: }
093: return result;
094: }
095:
096: public int available() throws IOException {
097: if (DebugFile.trace)
098: DebugFile
099: .writeln("Named Pipe available() does not apply to TRANSACT Named Pipes");
100: return 0;
101: }
102:
103: int receive(byte[] b, int off, int len) {
104: int i;
105:
106: if (len > (pipe_buf.length - used)) {
107: byte[] tmp;
108: int new_size;
109:
110: new_size = pipe_buf.length * 2;
111: if (len > (new_size - used)) {
112: new_size = len + used;
113: }
114: tmp = pipe_buf;
115: pipe_buf = new byte[new_size];
116: i = tmp.length - beg_idx;
117: if (used > i) {
118: System.arraycopy(tmp, beg_idx, pipe_buf, 0, i);
119: System.arraycopy(tmp, 0, pipe_buf, i, used - i);
120: nxt_idx = used;
121: } else {
122: System.arraycopy(tmp, beg_idx, pipe_buf, 0, used);
123: }
124: beg_idx = 0;
125: tmp = null;
126: }
127:
128: i = pipe_buf.length - nxt_idx;
129: if (len > i) {
130: System.arraycopy(b, off, pipe_buf, nxt_idx, i);
131: off += i;
132: System.arraycopy(b, off, pipe_buf, 0, len - i);
133: } else {
134: System.arraycopy(b, off, pipe_buf, nxt_idx, len);
135: }
136: nxt_idx = (nxt_idx + len) % pipe_buf.length;
137: used += len;
138: return len;
139: }
140: }
|