001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    
033    package com.imagero.uio.io;
034    
035    
036    import com.imagero.uio.Sys;
037    
038    import java.io.OutputStream;
039    import java.util.Vector;
040    
041    /**
042     * MultiByteArrayOutputStream.java
043     * <br>
044     * It's like ByteArrayOutputStream, but with multiple arrays <br>
045     * Array size is defined through <code>sizeX</code>;
046     *
047     * @author Kouznetsov Andrei
048     */
049    public class MultiByteArrayOutputStream extends OutputStream {
050    
051        Vector v;
052        int totalCount;
053    
054        int sizeX = 1024;
055    
056        protected byte buf[];
057    
058        protected int pos;
059    
060    
061        public MultiByteArrayOutputStream() {
062            this(1024);
063        }
064    
065        public MultiByteArrayOutputStream(int sizeX) {
066            this.sizeX = sizeX;
067            this.v = new Vector();
068            nextArray();
069        }
070    
071        protected void nextArray() {
072            byte[] b = new byte[this.sizeX];
073            v.addElement(b);
074            this.buf = b;
075            this.pos = 0;
076        }
077    
078        public synchronized void write(byte b[], int off, int len) {
079            for(int i = off; i < len; i++) {
080                write(b[off + i]);
081            }
082        }
083    
084        public synchronized void write(int b) {
085            if(pos == sizeX) {
086                nextArray();
087            }
088            totalCount++;
089            buf[pos++] = (byte) (b & 0xFF);
090        }
091    
092        public static void printHex(int value) {
093            value = value & 0xFF;
094            String s = Integer.toHexString(value);
095            if(s.length() == 1) {
096                Sys.out.print("0");
097            }
098            Sys.out.print(s);
099            Sys.out.print(" ");
100        }
101    
102        public void reset() {
103            totalCount = 0;
104            v = new Vector();
105            nextArray();
106        }
107    
108        public Vector getVector() {
109    
110            int lastIndex = this.v.size() - 1;
111            byte[] b = (byte[]) this.v.elementAt(lastIndex);
112            byte[] b2 = new byte[pos];
113            System.arraycopy(b, 0, b2, 0, pos);
114            this.v.setElementAt(b2, lastIndex);
115    
116            return this.v;
117        }
118    
119        public void flush() {
120        }
121    
122        public void close() {
123        }
124    
125        public int length() {
126            return (this.v.size() - 1) * this.sizeX + this.pos;
127        }
128    }