Files
alfresco-community-repo/source/java/org/alfresco/util/remote/RemotableInputStream.java
Alan Davis 91eb2644ad Merged 5.2.N (5.2.1) to HEAD (5.2)
125781 rmunteanu: Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
      125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
         125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127808 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-06-03 16:40:56 +00:00

120 lines
3.6 KiB
Java

package org.alfresco.util.remote;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.InetAddress;
import org.alfresco.util.remote.server.RemoteInputStreamServer;
import org.alfresco.util.remote.server.RmiRemoteInputStreamServer;
/**
* The data consuming side of the remote connection that the <code>InputStream</code> spans.
*
* @author <a href="mailto:Michael.Shavnev@effective-soft.com">Michael Shavnev</a>
* @since Alfresco 2.2
*/
public class RemotableInputStream extends InputStream implements Serializable
{
private static final long serialVersionUID = 2434858590717000057L;
private int port;
private String host;
private String name;
transient private RemoteInputStreamServer inputStreamServer;
public RemotableInputStream(String host, int port, InputStream inputStream)
{
this.host = host;
this.port = port;
this.inputStreamServer = new RmiRemoteInputStreamServer(inputStream);
}
public void close() throws IOException
{
inputStreamServer.close();
}
public int read() throws IOException
{
return inputStreamServer.read();
}
public int read(byte[] bytes) throws IOException
{
return inputStreamServer.read(bytes);
}
public int read(byte[] bytes, int off, int len) throws IOException
{
return inputStreamServer.read(bytes, off, len);
}
public long skip(long n) throws IOException
{
return inputStreamServer.skip(n);
}
public int available() throws IOException
{
return inputStreamServer.available();
}
public void mark(int readlimit)
{
inputStreamServer.mark(readlimit);
}
public boolean markSupported()
{
return inputStreamServer.markSupported();
}
public void reset() throws IOException
{
inputStreamServer.reset();
}
private void writeObject(ObjectOutputStream out) throws IOException
{
name = inputStreamServer.start(host, port);
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
inputStreamServer = (RemoteInputStreamServer) RmiRemoteInputStreamServer.obtain(host, port, name);
}
public static void main(String[] args) throws Exception
{
RemotableInputStream remotableInputStream = new RemotableInputStream(InetAddress.getLocalHost().getHostName(), 7777, new ByteArrayInputStream("test".getBytes()));
for (int b = -1; (b = remotableInputStream.read()) != -1;)
{
System.out.println((char) b);
}
remotableInputStream = new RemotableInputStream(InetAddress.getLocalHost().getHostName(), 7777, new ByteArrayInputStream("test".getBytes()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(remotableInputStream);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
remotableInputStream = (RemotableInputStream) ois.readObject();
for (int b = -1; (b = remotableInputStream.read()) != -1;)
{
System.out.println((char) b);
}
remotableInputStream.close();
}
}