Wrapper omkring Socket:
package clisrv;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class EnhancedSocket {
public static interface Reader {
public byte readByte() throws IOException;
public byte[] readByteArray() throws IOException;
public String readString() throws IOException;
public InputStream readStream() throws IOException;
public void close() throws IOException;
}
public static interface Writer {
public void writeByte(byte b) throws IOException;
public void writeByteArray(byte[] ba) throws IOException;
public void writeString(String s) throws IOException;
public void writeStream(InputStream is) throws IOException;
public void close() throws IOException;
}
private static class ReaderImpl implements Reader {
private InputStream is;
public ReaderImpl(Socket s) throws IOException {
is = s.getInputStream();
}
public byte readByte() throws IOException {
return (byte)is.read();
}
public byte[] readByteArray() throws IOException {
int len = is.read();
byte[] ba = new byte[len];
int ix = 0;
while(ix < len) {
ix += is.read(ba, ix, ba.length - ix);
}
return ba;
}
public String readString() throws IOException {
byte[] ba = readByteArray();
return new String(ba, "UTF-8");
}
public InputStream readStream() throws IOException {
return is;
}
public void close() throws IOException {
is.close();
}
}
private static class WriterImpl implements Writer {
private OutputStream os;
public WriterImpl(Socket s) throws IOException {
os = s.getOutputStream();
}
public void writeByte(byte b) throws IOException {
os.write(b);
os.flush();
}
public void writeByteArray(byte[] ba) throws IOException {
if(ba.length > 255) throw new IllegalArgumentException("Byte array or string too long: " + ba.length);
os.write(ba.length);
os.write(ba);
os.flush();
}
public void writeString(String s) throws IOException {
writeByteArray(s.getBytes("UTF-8"));
}
public void writeStream(InputStream is) throws IOException {
streamCopy(is, os);
}
public void close() throws IOException {
os.close();
}
}
public static Reader getReader(Socket s) throws IOException {
return new ReaderImpl(s);
}
public static Writer getWriter(Socket s) throws IOException {
return new WriterImpl(s);
}
public static void streamCopy(InputStream is, OutputStream os) throws IOException {
byte[] ba = new byte[65536];
int n;
while((n = is.read(ba, 0, ba.length)) > 0) {
os.write(ba, 0, n);
}
os.flush();
is.close();
}
}
Server:
package clisrv;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server1 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
EnhancedSocket.Writer wrt = EnhancedSocket.getWriter(s);
EnhancedSocket.Reader rdr = EnhancedSocket.getReader(s);
String fnm = "C:/work/" + rdr.readString();
if(new File(fnm).exists()) {
wrt.writeString("OK");
wrt.writeStream(new FileInputStream(fnm));
} else {
wrt.writeString("Error");
}
rdr.close();
wrt.close();
}
}
Client:
package clisrv;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client1 {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 12345);
EnhancedSocket.Writer wrt = EnhancedSocket.getWriter(s);
EnhancedSocket.Reader rdr = EnhancedSocket.getReader(s);
wrt.writeString("z1.zip");
String sts = rdr.readString();
if(sts.equals("OK")) {
OutputStream f = new FileOutputStream("C:/work/z2.zip");
EnhancedSocket.streamCopy(rdr.readStream(), f);
f.close();
}
rdr.close();
wrt.close();
}
}