Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
 /*
  * Created on Dec 15, 2005
  */
  
 package org.jboss.remoting.transport.multiplex.utility;
 
 
 import  org.jboss.logging.Logger;


GrowablePipedInputStream is the parent of the MultiplexingInputStream returned by VirtualSocket.getInputStream(). GrowablePipedInputStream and GrowablePipedOutputStream work together like java.io.PipedInputStream and java.io.PipedOutputStream, so that calling GrowablePipedOutputStream.write() causes bytes to be deposited with the matching GrowablePipedInputStream. However, unlike PipedInputStream, GrowablePipedInputStream stores bytes in a ShrinkableByteArrayOutputStream, which can grow and contract dynamically in response to the number of bytes it contains.

For more information about method behavior, see the java.io.InputStream javadoc.

Deprecated:
As of release 2.4.0 the multiplex transport will no longer be actively supported.
Author(s):
Ron Sigal
Version:
$Revision: 3443 $

Copyright (c) 2005

 
 
 public class GrowablePipedInputStream extends InputStream
 {
    protected static final Logger log = Logger.getLogger(GrowablePipedInputStream.class);
    private boolean connected;
    private int timeout;

   
Create a new GrowablePipedInputStream.
 
    public GrowablePipedInputStream()
    {
    }
   
Create a new GrowablePipedInputStream.

Parameters:
virtualSelector
 
    public GrowablePipedInputStream(VirtualSelector virtualSelector)
    {
       this. = virtualSelector;
    }


Create a new GrowablePipedInputStream.

Parameters:
src
Throws:
java.io.IOException
 
    {
       this. = source;
       source.connect(this);
        = true;
    }
   
      
Create a new GrowablePipedInputStream.

Parameters:
virtualSelector
src
Throws:
java.io.IOException
   public GrowablePipedInputStream(GrowablePipedOutputStream sourceVirtualSelector virtualSelectorthrows IOException
   {
      this. = source;
      this. = virtualSelector;
      source.connect(this);
       = true;
   }
   
   public synchronized int available()
   {
      return .available();
   }
   
   
   public void close() throws IOException
   {
      super.close();
      if ( != null)
         .unregister(this);
   }
   
   
   public int getTimeout()
   {
      return ;
   }
   
   
   public synchronized int read() throws IOException
   {
      if (!)
         throw new IOException("Pipe not connected");
      
      if (.available() == 0)
      {
         long start = System.currentTimeMillis();
         
         while (true)
         {  
            try
            {
               .trace(this + ": entering wait()");
               wait();
               .trace("leaving wait()");
               
               if (.available() > 0)
                  break;
               
               if (0 <  &&  <= System.currentTimeMillis() - start)
                  throw new SocketTimeoutException("Read timed out");
            }
            catch (InterruptedException ignored)
            {
               .debug("interrupted");
               throw new InterruptedIOException();
            }
         }
      }
      
      byte[] bytes = .toByteArray(1);
      int answer =  0xff & bytes[.start()];
      
      if (.available() > 0)
         notify();
      
      return answer;
   }
   
   
   public synchronized int read(byte[] bytesthrows IOException
   {
      return read(bytes, 0, bytes.length);
   }
   
   
   public synchronized int read(byte[] bytesint offsetint lengththrows IOException
   {
      if (!)
         throw new IOException("Pipe not connected");
      if (.available() == 0)
      {
         long start = System.currentTimeMillis();
         
         while (true)
         {  
            try
            {
               .trace(this + ": entering wait()");
               wait();
               .trace("leaving wait()");
               
               if (.available() > 0)
                  break;
               
               if (0 <  &&  <= System.currentTimeMillis() - start)
                  throw new SocketTimeoutException("Read timed out");
            }
            catch (InterruptedException ignored)
            {
               .debug("interrupted");
               throw new InterruptedIOException();
            }
         }
      }
      
      byte[] localBytes = .toByteArray(length);
      int from = .start();
      int n = .bytesReturned();
      System.arraycopy(localBytesfrombytesoffsetn);
      
      if (.available() > 0)
         notify();
      
      return n;
   }
   
   
   public void register(VirtualSelector virtualSelectorObject attachment)
   {
      this. = virtualSelector;
      virtualSelector.register(thisattachment);
   }
   
   
   public void setTimeout(int timeout)
   {
      this. = timeout;
   }
   
   
   protected void connect(GrowablePipedOutputStream sourcethrows IOException
   {
      if (source == null)
         throw new NullPointerException();
      
      if (source.isConnected())
         throw new IOException("Already connected");
      
      this. = source;
       = true;
   }
   
   
   protected boolean isConnected()
   {
      return ;
   }
   
   
   protected void receive(int ithrows IOException
   {
      .trace("entering receive()");
      synchronized (this)
      {
         .write(i);
         notify();
      }
      
      if ( != null)
   }
   
   
   protected void receive(byte[] bytesthrows IOException
   {
      .trace("entering receive()");
      synchronized (this)
      {
         .write(bytes);
         notify();
      }
      
      if ( != null)
   }
   
   
   protected void receive(byte[] bytesint offsetint lengththrows IOException
   {
      .trace(this + ": entering receive()");
      synchronized (this)
      {
         .write(bytesoffsetlength);
         .trace(this + ": notifying");
         notify();
      }
      
      if ( != null)
   }
New to GrepCode? Check out our FAQ X