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    import java.io.FilterInputStream;
036    import java.io.IOException;
037    import java.io.InputStream;
038    
039    /**
040     * @author Andrey Kuznetsov
041     */
042    public abstract class RLEInputStream extends FilterInputStream {
043        boolean finished;
044    
045        public RLEInputStream(InputStream in) {
046            super(in);
047        }
048    
049        public int available() throws IOException {
050            if (finished) {
051                return 0;
052            }
053            return 1;
054        }
055    
056        public void close() throws IOException {
057        }
058    
059        public boolean markSupported() {
060            return false;
061        }
062    
063        public synchronized void mark(int readlimit) {
064        }
065    
066        public synchronized void reset() throws IOException {
067            throw new IOException("mark/reset not supported");
068        }
069    
070        public int read(byte b[]) throws IOException {
071            return read(b, 0, b.length);
072        }
073    
074        public int read(byte b[], int off, int len) throws IOException {
075            int i = off;
076    
077            try {
078                for (; i < off + len; i++) {
079                    int a = read();
080                    if (a == -1) {
081                        i--;
082                        break;
083                    }
084                    b[i] = (byte) a;
085                }
086            }
087            catch (IOException ex) {
088                ex.printStackTrace();
089            }
090            return i - off;
091        }
092    
093        public abstract int read() throws IOException;
094    
095        public static class EndOfLineException extends IOException {
096            public EndOfLineException() {
097                super("RowEndException");
098            }
099        }
100    
101        public static class EndOfBitmapException extends IOException {
102            public EndOfBitmapException() {
103                super("BitmapEndException");
104            }
105        }
106    
107        public static class DeltaRecordException extends IOException {
108            public final int dx;
109            public final int dy;
110    
111            public DeltaRecordException(int dx, int dy) {
112                this.dx = dx;
113                this.dy = dy;
114            }
115        }
116    }