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    package com.imagero.uio.impl.array;
033    
034    import com.imagero.uio.RandomAccessIO;
035    import com.imagero.uio.RandomAccessInput;
036    import com.imagero.uio.RandomAccessOutput;
037    import com.imagero.uio.bio.BufferPosition;
038    import com.imagero.uio.bio.IOController;
039    import com.imagero.uio.bio.content.Content;
040    import com.imagero.uio.bio.content.IntArrayContent;
041    import com.imagero.uio.Transformer;
042    import com.imagero.uio.xform.XtoByteBE;
043    import com.imagero.uio.xform.XtoByteLE;
044    
045    import java.io.IOException;
046    
047    /**
048     * @author Andrey Kuznetsov
049     */
050    public class RandomAccessIntArray extends AbstractRandomAccessArray {
051    
052        int[] buf;
053    
054        public RandomAccessIntArray(int[] buf, int byteOrder) {
055            this(buf, 0, buf.length, byteOrder);
056        }
057    
058        public RandomAccessIntArray(int[] buf, int offset, int length, int byteOrder) {
059            this(buf, offset, length, byteOrder, new BufferPosition(buf.length * 4));
060        }
061    
062        public RandomAccessIntArray(int[] buf, int offset, int length, int byteOrder, BufferPosition fp) {
063            super(offset, length, byteOrder, fp);
064            this.buf = buf;
065        }
066    
067        public int read() throws IOException {
068            int k = /*offset + */fp.pos / bytesPerUnit;
069            if (k >= buf.length) {
070                return -1;
071            }
072            return (buf[k] >> shifts[(fp.pos++ & andMask)]) & 0xFF;
073        }
074    
075        protected int[] createShiftBE() {
076            return new int[]{24, 16, 8, 0};
077        }
078    
079        protected int[] createShiftLE() {
080            return new int[]{0, 8, 16, 24};
081        }
082    
083        protected int getBytesPerUnit() {
084            return 4;
085        }
086    
087        protected int getAndMask() {
088            return 3;
089        }
090    
091        protected void readBE(int srcOffset, int count, byte[] dest, int off) {
092            XtoByteBE.intToByte(buf, srcOffset, count, dest, off);
093        }
094    
095        protected void readLE(int srcOffset, int count, byte[] dest, int off) {
096            XtoByteLE.intToByte(buf, srcOffset, count, dest, off);
097        }
098    
099        public void write(int b) throws IOException {
100            buf[offset + fp.pos] = combine(buf[/*offset + */fp.pos], b);
101            fp.pos++;
102        }
103    
104        protected void write(byte[] src, int srcOffset, int len, int destOffset, boolean bigEndian) {
105            Transformer.byteToInt(src, srcOffset, len, buf, destOffset, bigEndian);
106        }
107    
108        public RandomAccessIO createIOChild(long offset, int byteOrder, boolean syncPointer) throws IOException {
109            long _offset = this.offset + offset;
110            return new RandomAccessIntArray(buf, (int) _offset, (int) (buf.length - _offset), byteOrder, syncPointer?fp : new BufferPosition((int) (buf.length - _offset) * 4));
111        }
112    
113        public RandomAccessInput createInputChild(long offset, int byteOrder, boolean syncPointer) throws IOException {
114            long _offset = this.offset + offset;
115            return new RandomAccessIntArray(buf, (int) _offset, (int) (buf.length - _offset), byteOrder);
116        }
117    
118        public RandomAccessOutput createOutputChild(long offset, int byteOrder, boolean syncPointer) throws IOException {
119            return createIOChild(offset, byteOrder, syncPointer);
120        }
121    
122        protected IOController createController() {
123            Content content = new IntArrayContent(new int[][] {buf});
124            return new IOController(com.imagero.uio.UIOStreamBuilder.DEFAULT_CHUNK_SIZE, content);
125        }
126    }