/*-
 * Copyright (C) 2008 Erik Larsson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.catacombae.util;

import org.catacombae.io.ReadableRandomAccessStream;
import org.catacombae.io.RuntimeIOException;

/**
 * CatacombaeIO-specific utility class.
 * 
 * @author Erik Larsson
 */
public class IOUtil {
    /**
     * Reads the supplied ReadableRandomAccessStream from its current position
     * until the end of the stream.
     *
     * @param s
     * @return the contents of the remainder of the stream.
     * @throws org.catacombae.io.RuntimeIOException if an I/O error occurred
     * when reading the stream.
     */
    public static byte[] readFully(ReadableRandomAccessStream s) throws RuntimeIOException {
        if(s.length() < 0 || s.length() > Integer.MAX_VALUE)
            throw new IllegalArgumentException("Length of s is out of range: " + s.length());

        byte[] res = new byte[(int)(s.length()-s.getFilePointer())];
        s.readFully(res);
        return res;
    }
}

@@ -1395,16 +1395,28 @@
 tream s)
+%0A           
  throws 
@@ -1433,16 +1433,20 @@
 xception
+%0A   
  %7B%0A     
@@ -1500,16 +1500,18 @@
 X_VALUE)
+ %7B
 %0A       
@@ -1587,91 +1587,1086 @@
  %22 +
- s.length());%0A%0A        byte%5B%5D res = new byte%5B(int)(s.length()-s.getFilePointer())%5D;
+%0A                    s.length());%0A        %7D%0A%0A        return readFully(s, null, (int) s.length());%0A    %7D%0A%0A    public static byte%5B%5D readFully(ReadableRandomAccessStream s, long offset,%0A            int length) throws RuntimeIOException%0A    %7B%0A        return readFully(s, Long.valueOf(offset), length);%0A    %7D%0A%0A    private static byte%5B%5D readFully(ReadableRandomAccessStream s, Long offset,%0A            int length) throws RuntimeIOException%0A    %7B%0A        long trueOffset;%0A%0A        if(offset == null) %7B%0A            trueOffset = s.getFilePointer();%0A        %7D%0A        else %7B%0A            trueOffset = offset;%0A        %7D%0A%0A        if(length %3E s.length()) %7B%0A            throw new IllegalArgumentException(%22'length' is unreasonably %22 +%0A                    %22large: %22 + length);%0A        %7D%0A        else if((s.length() - length) %3C offset) %7B%0A            throw new IllegalArgumentException(%22Offset out of range: %22 +%0A                    offset + %22(length: %22 + s.length() + %22)%22);%0A        %7D%0A%0A        byte%5B%5D res = new byte%5Blength%5D;%0A%0A        if(offset != null) %7B%0A            s.seek(trueOffset);%0A        %7D%0A
 %0A   
@@ -2688,16 +2688,17 @@
 y(res);%0A
+%0A
         

Add new method IOUtil.readFully(ReadableRandomAccessStream, long, int).