001 /*
002 * This file is a part of the RMI Plugin for Eclipse tutorials.
003 * Copyright (C) 2002-7 Genady Beryozkin
004 *
005 * You are free to modify this file, as long as you leave
006 * the following copyright:
007 *
008 * This file is based on the Remote File System example of
009 * the RMI Plug-in for Eclipse. The original code is
010 * Copyright (C) 2002-7 Genady Beryozkin
011 */
012 package demo.rmi.filesystem.client;
013
014 import java.io.IOException;
015 import java.io.InputStream;
016
017 import demo.rmi.filesystem.common.IRemoteInputStream;
018
019 /**
020 * A wrapper for a remote input stream. It extends {@link InputStream}
021 * so it can be used in APIs that expect {@link InputStream} and can't
022 * work with our {@link IRemoteInputStream}. Notice the implementation
023 * of {@link RemoteInputStreamWrapper#read(byte[], int, int)} that must
024 * use {@link IRemoteInputStream#read(int)}.
025 *
026 * @author Genady Beryozkin, rmi-info@genady.net
027 */
028 public class RemoteInputStreamWrapper extends InputStream {
029
030 IRemoteInputStream is;
031
032 public RemoteInputStreamWrapper(IRemoteInputStream is) {
033 this.is = is;
034 }
035
036 @Override
037 public int read() throws IOException {
038 return is.read();
039 }
040
041 @Override
042 public int available() throws IOException {
043 return is.available();
044 }
045
046 @Override
047 public void close() throws IOException {
048 is.close();
049 }
050
051 @Override
052 public int read(byte[] b) throws IOException {
053 return read(b, 0, b.length);
054 }
055
056 /*
057 * (non-Javadoc)
058 * @see java.io.InputStream#read(byte[], int, int)
059 * @see IRemoteInputStream#read(int)
060 */
061 @Override
062 public int read(byte[] b, int off, int len) throws IOException {
063 byte[] buf = is.read(len);
064 if (buf == null) {
065 return -1;
066 }
067 System.arraycopy(buf, 0, b, off, buf.length);
068 return buf.length;
069 }
070
071 @Override
072 public long skip(long n) throws IOException {
073 return is.skip(n);
074 }
075 }