// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
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();
    }
}
