package com.jscape.util.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Alex Usun
 */
public interface StreamProtectionService {

    StreamProtectionService NULL = new StreamProtectionService() {

        @Override
        public InputStream protect(InputStream base) {
            return base;
        }

        @Override
        public InputStream unprotect(InputStream base) {
            return base;
        }

        @Override
        public OutputStream protect(OutputStream base) {
            return base;
        }

        @Override
        public OutputStream unprotect(OutputStream base) {
            return base;
        }
    };

    InputStream protect(InputStream base)
            throws IOException;

    InputStream unprotect(InputStream base)
            throws IOException;

    OutputStream protect(OutputStream base)
            throws IOException;

    OutputStream unprotect(OutputStream base)
            throws IOException;

    class OperationException
            extends IOException {

        public static OperationException wrap(Throwable e)
                throws OperationException {
            throw e instanceof OperationException ? (OperationException) e : new OperationException(e);
        }

        public OperationException() {
        }

        public OperationException(String message) {
            super(message);
        }

        public OperationException(String message, Throwable cause) {
            super(message, cause);
        }

        public OperationException(Throwable cause) {
            super(cause.getMessage() != null ? cause.getMessage() : cause.toString(), cause);
        }
    }

}
