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.io;
033
034 import java.io.ByteArrayInputStream;
035
036 /**
037 * Like byteArrayInputStream but works with 2d byte array
038 * @author Andrey Kuznetsov
039 */
040 public class ByteArray2DInputStream extends ByteArrayInputStream {
041
042 private byte[][] data;
043
044 int current;
045 boolean finished;
046
047 int markIndex;
048
049 public ByteArray2DInputStream(byte[][] data) {
050 super(data[0]);
051 this.data = data;
052 }
053
054 public int read() {
055 if (finished) {
056 return -1;
057 }
058 if (pos >= count) {
059 next();
060 }
061 return super.read();
062 }
063
064 private void next() {
065 if ((current + 1) < data.length) {
066 buf = data[++current];
067 pos = 0;
068 count = buf.length;
069 }
070 else {
071 finished = true;
072 }
073 }
074
075 public int read(byte[] buf, int off, int len) {
076 int read = 0;
077 while (read < len && !finished) {
078 if (pos >= count) {
079 next();
080 }
081 int rd = super.read(buf, off + read, len - read);
082 if(rd <= 0) {
083 break;
084 }
085 read += rd;
086 }
087 return read;
088 }
089
090 public long skip(long ns) {
091 long skipped = 0;
092 while (skipped < ns && !finished) {
093 if (pos >= count) {
094 next();
095 }
096 long skp = super.skip(ns - skipped);
097 skipped += skp;
098 if (skp == 0) {
099 break;
100 }
101 }
102 return skipped;
103 }
104
105 public void mark(int readAheadLimit) {
106 markIndex = current;
107 super.mark(readAheadLimit);
108 }
109
110 public void reset() {
111 buf = data[markIndex];
112 super.reset();
113 finished = false;
114 }
115 }