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.netbios;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023:
024: class SocketInputStream extends InputStream {
025:
026: private static final int TMP_BUFFER_SIZE = 256;
027:
028: private InputStream in;
029: private SessionServicePacket ssp;
030: private int tot, bip, n;
031: private byte[] header, tmp;
032:
033: SocketInputStream(InputStream in) {
034: this .in = in;
035: header = new byte[4];
036: tmp = new byte[TMP_BUFFER_SIZE];
037: }
038:
039: public synchronized int read() throws IOException {
040: if (read(tmp, 0, 1) < 0) {
041: return -1;
042: }
043: return tmp[0] & 0xFF;
044: }
045:
046: public synchronized int read(byte[] b) throws IOException {
047: return read(b, 0, b.length);
048: }
049:
050: /* This method will not return until len bytes have been read
051: * or the stream has been closed.
052: */
053:
054: public synchronized int read(byte[] b, int off, int len)
055: throws IOException {
056: if (len == 0) {
057: return 0;
058: }
059: tot = 0;
060:
061: while (true) {
062: while (bip > 0) {
063: n = in.read(b, off, Math.min(len, bip));
064: if (n == -1) {
065: return tot > 0 ? tot : -1;
066: }
067: tot += n;
068: off += n;
069: len -= n;
070: bip -= n;
071: if (len == 0) {
072: return tot;
073: }
074: }
075:
076: switch (SessionServicePacket.readPacketType(in, header, 0)) {
077: case SessionServicePacket.SESSION_KEEP_ALIVE:
078: break;
079: case SessionServicePacket.SESSION_MESSAGE:
080: bip = SessionServicePacket.readLength(header, 0);
081: break;
082: case -1:
083: if (tot > 0) {
084: return tot;
085: }
086: return -1;
087: }
088: }
089: }
090:
091: public synchronized long skip(long numbytes) throws IOException {
092: if (numbytes <= 0) {
093: return 0;
094: }
095: long n = numbytes;
096: while (n > 0) {
097: int r = read(tmp, 0, (int) Math.min((long) TMP_BUFFER_SIZE,
098: n));
099: if (r < 0) {
100: break;
101: }
102: n -= r;
103: }
104: return numbytes - n;
105: }
106:
107: public int available() throws IOException {
108: if (bip > 0) {
109: return bip;
110: }
111: return in.available();
112: }
113:
114: public void close() throws IOException {
115: in.close();
116: }
117: }
|