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 jcifs.smb;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023: import java.net.UnknownHostException;
024: import java.net.MalformedURLException;
025:
026: class TransactNamedPipeInputStream extends SmbFileInputStream {
027:
028: private static final int INIT_PIPE_SIZE = 4096;
029:
030: private byte[] pipe_buf = new byte[INIT_PIPE_SIZE];
031: private int beg_idx, nxt_idx, used;
032: private boolean dcePipe;
033:
034: Object lock;
035:
036: TransactNamedPipeInputStream(SmbNamedPipe pipe)
037: throws SmbException, MalformedURLException,
038: UnknownHostException {
039: super (pipe, (pipe.pipeType & 0xFFFF0000) | SmbFile.O_EXCL);
040: this .dcePipe = (pipe.pipeType & SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT) == SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT;
041: lock = new Object();
042: }
043:
044: public int read() throws IOException {
045: int result = -1;
046:
047: synchronized (lock) {
048: try {
049: while (used == 0) {
050: lock.wait();
051: }
052: } catch (InterruptedException ie) {
053: throw new IOException(ie.getMessage());
054: }
055: result = pipe_buf[beg_idx] & 0xFF;
056: beg_idx = (beg_idx + 1) % pipe_buf.length;
057: }
058: return result;
059: }
060:
061: public int read(byte[] b) throws IOException {
062: return read(b, 0, b.length);
063: }
064:
065: public int read(byte[] b, int off, int len) throws IOException {
066: int result = -1;
067: int i;
068:
069: if (len <= 0) {
070: return 0;
071: }
072: synchronized (lock) {
073: try {
074: while (used == 0) {
075: lock.wait();
076: }
077: } catch (InterruptedException ie) {
078: throw new IOException(ie.getMessage());
079: }
080: i = pipe_buf.length - beg_idx;
081: result = len > used ? used : len;
082: if (used > i && result > i) {
083: System.arraycopy(pipe_buf, beg_idx, b, off, i);
084: off += i;
085: System.arraycopy(pipe_buf, 0, b, off, result - i);
086: } else {
087: System.arraycopy(pipe_buf, beg_idx, b, off, result);
088: }
089: used -= result;
090: beg_idx = (beg_idx + result) % pipe_buf.length;
091: }
092:
093: return result;
094: }
095:
096: public int available() throws IOException {
097: if (file.log.level > 2)
098: file.log
099: .println("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) { /* 2 chunks */
118: System.arraycopy(tmp, beg_idx, pipe_buf, 0, i);
119: System.arraycopy(tmp, 0, pipe_buf, i, used - i);
120: } else {
121: System.arraycopy(tmp, beg_idx, pipe_buf, 0, used);
122: }
123: beg_idx = 0;
124: nxt_idx = used;
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:
141: public int dce_read(byte[] b, int off, int len) throws IOException {
142: return super.read(b, off, len);
143: }
144: }
|