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.bio.content;
033    
034    import java.io.IOException;
035    
036    /**
037     * Date: 05.01.2008
038     *
039     * @author Andrey Kuznetsov
040     */
041    public class ByteArrayContent extends Content {
042        byte[][] data;
043    
044        public ByteArrayContent(byte[][] data) {
045            this.data = data;
046        }
047    
048        public int load(long offset, int bpos, byte[] buffer) throws IOException {
049            IndexAndStart ias = getIAS(offset);
050            if (ias == null) {
051                return 0;
052            }
053            int index = ias.index;
054            long start = ias.start;
055            int pos = (int) (offset - start);
056            byte[] src = data[index];
057            int toCopy = Math.min(src.length - pos, buffer.length - bpos);
058            if (toCopy > 0) {
059                System.arraycopy(src, pos, buffer, bpos, toCopy);
060                if ((toCopy < buffer.length - bpos)) {
061                    return toCopy + load(offset + toCopy, bpos + toCopy, buffer);
062                }
063            }
064            return toCopy;
065        }
066    
067        public void close() {
068        }
069    
070        private IndexAndStart getIAS(long offset) {
071            long start = 0;
072            for (int i = 0; i < data.length; i++) {
073                byte[] src = data[i];
074                int length = src.length;
075                long end = start + length;
076                if (offset >= start && offset <= end) {
077                    return new IndexAndStart(i, start);
078                }
079                start += length;
080            }
081            return null;
082        }
083    
084        public void save(long offset, int spos, byte[] src, int length) throws IOException {
085            IndexAndStart ias = getIAS(offset);
086            if (ias == null) {
087                return;
088            }
089            int index = ias.index;
090            long start = ias.start;
091            int dpos = (int) (offset - start);
092            byte[] dest = data[index];
093            int request = Math.min(length, src.length - spos);
094            int toCopy = Math.min(dest.length - dpos, request);
095            if (request > 0 && toCopy > 0) {
096                System.arraycopy(src, spos, dest, dpos, toCopy);
097                if (toCopy < request) {
098                    save(offset + toCopy, spos + toCopy, src, request - toCopy);
099                }
100            }
101        }
102    
103        public boolean canReload() {
104            return true;
105        }
106    
107        public long length() throws IOException {
108            long length = 0;
109            for (int i = 0; i < data.length; i++) {
110                byte[] dest = data[i];
111                length += dest.length;
112            }
113            return length;
114        }
115    
116        public boolean writable() {
117            return true;
118        }
119    }