Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 import java.io.Writer;
 
 
 /* ------------------------------------------------------------ */
Byte Array ISO 8859 writer. This class combines the features of a OutputStreamWriter for ISO8859 encoding with that of a ByteArrayOutputStream. It avoids many inefficiencies associated with these standard library classes. It has been optimized for standard ASCII characters.
 
 public class ByteArrayISO8859Writer extends Writer
 {
     private byte[] _buf;
     private int _size;
     private ByteArrayOutputStream2 _bout=null;
     private OutputStreamWriter _writer=null;
     private boolean _fixed=false;
 
     /* ------------------------------------------------------------ */
    
Constructor.
 
     public ByteArrayISO8859Writer()
     {
         =new byte[2048];
     } 
     
     /* ------------------------------------------------------------ */
    
Constructor.

Parameters:
capacity Buffer capacity
 
     public ByteArrayISO8859Writer(int capacity)
     {
         =new byte[capacity];
     }
     
     /* ------------------------------------------------------------ */
     public ByteArrayISO8859Writer(byte[] buf)
     {
         =buf;
         =true;
     }
 
     /* ------------------------------------------------------------ */
     public Object getLock()
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
     public int size()
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
     public int capacity()
     {
         return .;
     }
 
     /* ------------------------------------------------------------ */
     public int spareCapacity()
     {
         return .-;
     }
     
     /* ------------------------------------------------------------ */
     public void setLength(int l)
     {
         =l;
     }
 
     /* ------------------------------------------------------------ */
     public byte[] getBuf()
     {
        return ;
    }
    
    /* ------------------------------------------------------------ */
    public void writeTo(OutputStream out)
        throws IOException
    {
        out.write(,0,);
    }
    /* ------------------------------------------------------------ */
    public void write(char c)
        throws IOException
    {
        ensureSpareCapacity(1);
        if (c>=0&&c<=0x7f)
            [++]=(byte)c;
        else
        {
            char[] ca ={c};
            writeEncoded(ca,0,1);
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void write(char[] ca)
        throws IOException
    {
        ensureSpareCapacity(ca.length);
        for (int i=0;i<ca.length;i++)
        {
            char c=ca[i];
            if (c>=0&&c<=0x7f)
                [++]=(byte)c;
            else
            {
                writeEncoded(ca,i,ca.length-i);
                break;
            }
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void write(char[] ca,int offsetint length)
        throws IOException
    {
        ensureSpareCapacity(length);
        for (int i=0;i<length;i++)
        {
            char c=ca[offset+i];
            if (c>=0&&c<=0x7f)
                [++]=(byte)c;
            else
            {
                writeEncoded(ca,offset+i,length-i);
                break;
            }
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void write(String s)
        throws IOException
    {
        if (s==null)
        {
            write("null",0,4);
            return;
        }
        
        int length=s.length();
        ensureSpareCapacity(length);
        for (int i=0;i<length;i++)
        {
            char c=s.charAt(i);
            if (c>=0x0&&c<=0x7f)
                [++]=(byte)c;
            else
            {
                writeEncoded(s.toCharArray(),i,length-i);
                break;
            }
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void write(String s,int offsetint length)
        throws IOException
    {
        ensureSpareCapacity(length);
        for (int i=0;i<length;i++)
        {
            char c=s.charAt(offset+i);
            if (c>=0&&c<=0x7f)
                [++]=(byte)c;
            else
            {
                writeEncoded(s.toCharArray(),offset+i,length-i);
                break;
            }
        }
    }
    /* ------------------------------------------------------------ */
    private void writeEncoded(char[] ca,int offsetint length)
        throws IOException
    {
        if (==null)
        {
             = new ByteArrayOutputStream2(2*length);
             = new OutputStreamWriter(,.);
        }
        else
            .reset();
        .write(ca,offset,length);
        .flush();
        System.arraycopy(.getBuf(),0,,,.getCount());
        +=.getCount();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void flush()
    {}
    /* ------------------------------------------------------------ */
    public void resetWriter()
    {
        =0;
    }
    /* ------------------------------------------------------------ */
    @Override
    public void close()
    {}
    /* ------------------------------------------------------------ */
    public void destroy()
    {
        =null;
    }
    
    /* ------------------------------------------------------------ */
    public void ensureSpareCapacity(int n)
        throws IOException
    {
        if (+n>.)
        {
            if ()
                throw new IOException("Buffer overflow: "+.);
            byte[] buf = new byte[(.+n)*4/3];
            System.arraycopy(,0,buf,0,);
            =buf;
        }
    }
    /* ------------------------------------------------------------ */
    public byte[] getByteArray()
    {
        byte[] data=new byte[];
        System.arraycopy(,0,data,0,);
        return data;
    }
    
}
    
    
New to GrepCode? Check out our FAQ X