NullStream.java

/*
** Module   : NullStream.java
** Abstract : represents a stream instance that is safe to call for any 
**            processing but which is a NOP
**
** Copyright (c) 2006-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------------------Description----------------------------
** 001 GES 20060323   @25207 Created initial version which represents a 
**                           stream instance that is safe to call for any 
**                           processing but which is a NOP.
** 002 NVS 20070725   @34687 Implemented methods isIn() and isOut(), which
**                           are newly added to the base class as abstract.
** 003 GES 20090625   @42944 Allow isIn() and isOut() to return true. This
**                           avoid read/write errors on "closed" streams which
**                           is how Progress silently deals with this case.
** 004 SVL 20110403          Added write(byte[]) function.
** 005 OM  20170622          Implemented peekCh() method.
** 006 ECF 20171026          Added write(byte[], int, int) method.
** 007 EVL 20220325          Base refactoring for Stream based classes getting single byte and array of bytes.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.util;

import java.io.*;

import com.goldencode.p2j.net.*;
import com.goldencode.p2j.security.ContextLocal;

/**
 * A stream class which represents a stream instance that is safe to call for  
 * any processing but which is in actuality a no operation.
 *
 * @author    GES
 */
public class NullStream
extends Stream
{
   /**
    * Create a default instance.    
    */
   public NullStream()
   {
      // no data, nothing to do
   }
   
   /**
    * The number of bytes available to be immediately read without blocking.
    *
    * @return   Always 0.
    */
   public long available()
   {
      return (long) 0;
   }
   
   /**
    * The 0-based offset into the stream at which the next read or write will 
    * occur.  
    *
    * @return   Always 0.
    */
   public long getPos()
   {
      return (long) 0;
   }
   
   /**
    * Moves the current read/write position to the specified absolute 0-based
    * offset, in actuality this has no effect. 
    *
    * @param    pos
    *           The new read/write position in the stream.
    */
   public void setPos(long pos)
   {
      // nothing to do
   }
   
   /**
    * State of the input side of the stream.
    *
    * @return   Always <code>true</code>.
    */
   public boolean isIn()
   {
      return true;
   }
   
   /**
    * State of the output side of the stream.
    *
    * @return   Always <code>true</code>.
    */
   public boolean isOut()
   {
      return true;
   }   
   
   /**
    * The length of the stream in bytes.
    *
    * @return   Always 0.
    */
   public long getLen()
   {
      return (long) 0;
   }
   
   /**
    * Truncates or extends the stream to the specified length if this stream
    * supports such an operation, in actuality this has no effect.
    *
    * @param    len
    *           Ignored.
    */
   public void setLen(long len)
   {
      // nothing to do
   }
   
   /**
    * Write the given character to the output stream, in actuality this has
    * no effect.
    *
    * @param    ch
    *           Ignored.
    */
   public void writeCh(char ch)
   {
      // nothing to do
   }
   
   /**
    * Write the given byte to the output stream, in actuality this has no
    * effect.
    *
    * @param    b
    *           Ignored.
    */
   public void writeByte(byte b)
   {
      // nothing to do
   }
   
   /**
    * Write the given string to the output stream, in actuality this has no 
    * effect.
    *
    * @param    data
    *           Ignored.
    */
   public void write(String data)
   {
      // nothing to do
   }
   
   /**
    * Write the given byte array to the output stream, in actuality this has
    * no effect.
    *
    * @param    data
    *           Ignored.
    */
   public void write(byte[] data)
   {
      // nothing to do
   }
   
   /**
    * Write the specified range of bytes from the given byte array to the output stream.
    * Implemented as a no-op.
    *
    * @param    data
    *           The data to be written.
    * @param    off
    *           Starting offset in data from which to read bytes to be written. Must be
    *           non-negative and {@code &lt; data.length}.
    * @param    len
    *           Length of data to be written. Must be non-negative and {@code &lt;= (data.length
    *           - offset)}.
    *
    * @throws   IOException
    *           If an I/O error occurs.
    */
   public void write(byte[] data, int off, int len)
   throws IOException
   {
      // nothing to do
   }
   
   /**
    * Read a character from the current read position in the stream.
    *
    * @return   Always -2 (simulates an {@code EOF}).
    */
   public int peekCh()
   {
      return -2;
   }
   
   /**
    * Read a character from the current read position in the stream.
    *
    * @return   Always -2 (simulates an <code>EOF</code>).
    */
   public int readCh()
   {
      return -2;
   }
   
   /**
    * Read a single byte from the underlying stream.
    * 
    * @return   A single byte from the stream, -1 on any failure and -2 upon an {@code EOF}.
    */
   @Override
   public int readByte()
   {
      return -2;
   }
   
   /**
    * Read all characters from the current read position in the stream to the
    * next line separator (as determined by the <code>File.separator</code>
    * or to the <code>EOF</code>. Any line separator character(s) and the
    * <code>EOF</code> character are not returned.  In actuality this has no
    * effect.
    *
    * @return   Always throws an <code>EOFException</code>.
    */
   public String readLn()
   throws EOFException
   {
      throw new EOFException("There is no stream.");
   }
   
   /**
    * Closes the input stream and releases OS resources associated with it,
    * in actuality this has no effect.
    */
   public void closeIn()
   {
      // nothing to do
   }
   
   /**
    * Closes the output stream and releases OS resources associated with it,
    * in actuality this has no effect.
    */
   public void closeOut()
   {
      // nothing to do
   }
   
   /**
    * Closes the stream and releases OS resources associated with it, in
    * actuality this has no effect.
    */
   public void close()
   {
      // nothing to do
   }
   
   /**
    * Assigns the internal stream reference to the given reference, in 
    * actuality this has no effect.
    *
    * @param    s
    *           The new internal stream reference to use for all operations,
    *           ignored.
    */
   public void assign(Stream s)
   {
      // nothing to do
   }   
   
   /**
    * Perform common close <b>preprocessing</b> for all streams, in actuality
    * this has no effect.
    *
    * @param    in
    *           Ignored.
    * @param    out
    *           Ignored.
    */
   protected void cleanup(boolean in, boolean out)
   throws IOException
   {
      // nothing to do
   }
   
   /**
    * Detects when a page break is needed.
    *
    * @return   Always <code>false</code>.
    */
   boolean needsPageBreak()
   {
      return false;
   }
   
   /**
    * Gets the current page size in lines.
    *
    * @return   Always 0.
    */
   int rawGetPageSize()
   {
      return 0;
   }
   
   /**
    * Sets the current page size in lines, in actuality this has no effect.
    *
    * @param    sz
    *           Ignored.
    */
   void rawSetPageSize(int sz)
   {
      // nothing to do
   }
   
   /**
    * Gets the current page number.
    *
    * @return   Always 0.
    */
   int rawGetPageNum()
   {
      return 0;
   }
   
   /**
    * Increments the current page number, in actuality this has no effect.
    */
   void rawIncrementPageNum()
   {
      // nothing to do
   }
   
   /**
    * Gets the current page's next line number.
    *
    * @return   Always 0.
    */
   int rawGetNextLineNum()
   {
      return 0;
   }
   
   /**
    * Increments the current page's next line number, in actuality this has
    * no effect.
    */
   void rawIncrementNextLineNum()
   {
      // nothing to do
   }
   
   /**
    * Resets the current page's next line number to 1, in actuality this has
    * no effect.
    */
   void rawResetNextLineNum()
   {
      // nothing to do
   }
}