Package Framework

Source Code of Framework.PrintStreamToSeekStreamConverter

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package Framework;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.Serializable;

import javax.help.UnsupportedOperationException;

/**
* This class converts a PrintStream, such as System.err into a SeekStream subclass. This can be used when desiring the output to go
* either to a file or to standard output, for instance. As SeekStream is a generic input/output stream whereas print stream is only
* an output stream, the input style methods on this class throw <code>UnsupportedOperationException</code>s
*/
public class PrintStreamToSeekStreamConverter extends SeekStream {
    /**
     * The print stream to where output will be delegated.
     */
    private PrintStream delegate;

    /**
     * Create a new SeekStream whose output is always re-directed to the passed print stream
     * @param pDelegate
     */
    public PrintStreamToSeekStreamConverter(PrintStream pDelegate) {
        this.delegate = pDelegate;
    }

    /**
     * Flush the output print stream
     */
    @Override
    public int flush() {
        this.delegate.flush();
        return 0;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int position() {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("position() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int readBinary(BinaryData target, int length) {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("readBinary() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int readLine(TextData target, boolean includeEOL) {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("readLine() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public Object readSerialized() {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("readSerialized() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int readText(TextData target, int length) {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("readText() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int seek(int position, int refPoint) {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("seek() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public void setOffset(int offset) {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("setOffset() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public int size() {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("size() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * This method is an input method which is unsupported for PrintStreamToSeekStreamConverters. This method will always
     * throw an <code>UnsupportedOperationException</code>
     */
    @Override
    public InputStream toInputStream() {
        UnsupportedOperationException errorVar = new UnsupportedOperationException("toInputStream() cannot be invoked on a print stream via the PrintStreamToSeekStreamConverter");
        ErrorMgr.addError(errorVar);
        throw errorVar;
    }

    /**
     * Write the passed integer to the print stream associated with this object (set in the constructor)
     */
    @Override
    public void write(int b) throws IOException {
        this.delegate.print(b);
    }

    /**
     * Write the passed binary data to the print stream associated with this object (set in the constructor). This
     * method will write the contents of the binary data to the print stream in a humanly-readable hex format.
     */
    @Override
    public int writeBinary(BinaryData source, int length) {
        byte[] bytes = source.toByteArray();
        int maxValue = (length <= 0) ? bytes.length : Math.min(length, bytes.length);
        for (int i = 0; i < maxValue; i++) {
            this.delegate.printf("%02x ", bytes[i]);
        }
        return 0;
    }

    /**
     * Write the passed text to the print stream associated with this object (set in the constructor). A line separator
     * will be written after the string is written. If pLength is <= 0 or pLength >= pSource.length(), the whole string
     * will be written, followed by a line terminator. Otherwise, just pLength characters of the string are written,
     * followed by a line terminator.  
     */
    @Override
    public int writeLine(String pSource, int pLength) {
        if (pLength <= 0 || pLength >= pSource.length()) {
            this.delegate.println(pSource);
            return pSource.length();
        }
        else {
            this.delegate.println(pSource.substring(0, pLength));
            return pLength;
        }
    }

    /**
     * Write the passed object to the print stream associated with this object (set in the constructor). This
     * method will write the contents of the serialised object to the print stream in a humanly-readable hex format.
     */
    @Override
    public void writeSerialized(Serializable object) {
        writeSerialized((Object)object);
    }

    /**
     * Write the passed text to the print stream associated with this object (set in the constructor). If pLength is <= 0
     * or pLength >= pSource.length(), the whole string will be written, otherwise, just pLength characters of the string
     * are written.
     */
    @Override
    public int writeText(String pSource, int pLength) {
        if (pLength == 0 || pLength >= pSource.length()) {
            this.delegate.print(pSource);
            return pSource.length();
        }
        else {
            this.delegate.print(pSource.substring(0, pLength));
            return pLength;
        }
    }

    /**
     * Write the passed object to the print stream associated with this object (set in the constructor). This
     * method will write the contents of the serialised object to the print stream in a humanly-readable hex format.
     */
    @Override
    public void writeSerialized(Object object) {
        MemoryStream m = new MemoryStream();
        BinaryData bd = new BinaryData();

        m.open(Constants.SP_AM_WRITE, true);
        m.writeSerialized(object);
        m.close();
        m.open(Constants.SP_AM_READ, true);
        m.readBinary(bd);
        m.close();
        writeBinary(bd);
    }
}
TOP

Related Classes of Framework.PrintStreamToSeekStreamConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.