All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.util.CommitHeaderOutputStream Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta5
Show newest version
package org.jboss.resteasy.util;

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

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class CommitHeaderOutputStream extends OutputStream
{
   protected OutputStream delegate;
   protected boolean isHeadersCommitted;
   protected CommitCallback headers;

   public CommitHeaderOutputStream(final OutputStream delegate, final CommitCallback headers)
   {
      this.delegate = delegate;
      this.headers = headers;
   }

   public CommitHeaderOutputStream()
   {
   }

   public OutputStream getDelegate()
   {
      return delegate;
   }

   public void setDelegate(OutputStream delegate)
   {
      this.delegate = delegate;
   }

   public void setHeaders(CommitCallback headers)
   {
      this.headers = headers;
   }

   public interface CommitCallback
   {
      void commit();
   }

   public synchronized void commit()
   {
      if (isHeadersCommitted) return;
      isHeadersCommitted = true;
      headers.commit();
   }

   @Override
   public void write(int i) throws IOException
   {
      commit();
      delegate.write(i);
   }

   @Override
   public void write(byte[] bytes) throws IOException
   {
      commit();
      delegate.write(bytes);
   }

   @Override
   public void write(byte[] bytes, int i, int i1) throws IOException
   {
      commit();
      delegate.write(bytes, i, i1);
   }

   @Override
   public void flush() throws IOException
   {
      commit();
      delegate.flush();
   }



   @Override
   public void close() throws IOException
   {
      commit();
      delegate.close();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy