001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), hosted at https://github.com/gunterze/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2011
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.util;
040
041import java.io.EOFException;
042import java.io.FileInputStream;
043import java.io.FileNotFoundException;
044import java.io.IOException;
045import java.io.InputStream;
046import java.io.OutputStream;
047import java.net.URL;
048
049/**
050 * @author Gunter Zeilinger <gunterze@gmail.com>
051 */
052public class StreamUtils {
053    
054    private static final int COPY_BUFFER_SIZE = 2048;
055    
056    public static void readFully(InputStream in, byte b[], int off, int len)
057            throws IOException {
058        if (off < 0 || len < 0 || off + len > b.length)
059            throw new IndexOutOfBoundsException();
060        while (len > 0) {
061            int count = in.read(b, off, len);
062            if (count < 0)
063                throw new EOFException();
064            off += count;
065            len -= count;
066        }
067    }
068    
069    public static  void skipFully(InputStream in, long n) throws IOException {
070        while (n > 0) {
071            long count = in.skip(n);
072            if (count <= 0)
073                throw new EOFException();
074            n -= count;
075        }
076    }
077
078    public static void copy(InputStream in, OutputStream out, byte buf[])
079            throws IOException {
080        int count;
081        while ((count = in.read(buf, 0, buf.length)) > 0)
082            if (out != null)
083                out.write(buf, 0, count);
084    }
085
086    public static  void copy(InputStream in, OutputStream out)
087            throws IOException {
088        copy(in, out, new byte[COPY_BUFFER_SIZE]);
089    }
090
091    public static  void copy(InputStream in, OutputStream out, int len,
092            byte buf[]) throws IOException {
093        if (len < 0)
094            throw new IndexOutOfBoundsException();
095        while (len > 0) {
096            int count = in.read(buf, 0, Math.min(len, buf.length));
097            if (count < 0)
098                throw new EOFException();
099            out.write(buf, 0, count);
100            len -= count;
101        }
102    }
103
104    public static  void copy(InputStream in, OutputStream out, int len)
105            throws IOException {
106        copy(in, out, len, new byte[Math.min(len, COPY_BUFFER_SIZE)]);
107    }
108
109    public static void copy(InputStream in, OutputStream out, int len,
110            int swapBytes, byte buf[]) throws IOException {
111        if (swapBytes == 1) {
112            copy(in, out, len, buf);
113            return;
114        }
115        if (!(swapBytes == 2 || swapBytes == 4))
116            throw new IllegalArgumentException("swapBytes: " + swapBytes);
117        if (len < 0 || (len % swapBytes) != 0)
118            throw new IllegalArgumentException("length: " + len);
119        int off = 0;
120        while (len > 0) {
121            int count = in.read(buf, off, Math.min(len, buf.length - off));
122            if (count < 0)
123                throw new EOFException();
124            len -= count;
125            count += off;
126            off = count % swapBytes;
127            count -= off;
128            switch (swapBytes) {
129            case 2:
130                ByteUtils.swapShorts(buf, 0, count);
131                break;
132            case 4:
133                ByteUtils.swapInts(buf, 0, count);
134                break;
135            case 8:
136                ByteUtils.swapLongs(buf, 0, count);
137                break;
138            }
139            out.write(buf, 0, count);
140            if (off > 0)
141                System.arraycopy(buf, count, buf, 0, off);
142        }
143    }
144
145    public static void copy(InputStream in, OutputStream out, int len,
146            int swapBytes) throws IOException {
147        copy(in, out, len, swapBytes,
148                new byte[Math.min(len, COPY_BUFFER_SIZE)]);
149    }
150
151    public static InputStream openFileOrURL(String name) throws IOException {
152        if (name.startsWith("resource:")) {
153            URL url = ResourceLocator.getResourceURL(name.substring(9), StreamUtils.class);
154            if (url == null)
155                throw new FileNotFoundException(name);
156            return url.openStream();
157        }
158        if (name.indexOf(':') < 2)
159            return new FileInputStream(name);
160        return new URL(name).openStream();
161    }
162}