|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DirectInputStream.java | 0% | 21.4% | 37.5% | 21.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.io; | |
| 36 | ||
| 37 | import java.io.*; | |
| 38 | ||
| 39 | /** | |
| 40 | * <p>An {@code InputStream} that supports reading directly into an {@code OutputStream}. This class | |
| 41 | * provides default implementations defined in terms of {@code InputStream} and {@code OutputStream} | |
| 42 | * methods. Subclasses can override (at least) {@link #readAll(OutputStream, byte[])} and | |
| 43 | * {@link #read(OutputStream, int, byte[])} to provide better implementations (by, for example, | |
| 44 | * not invoking {@link InputStream#read(byte[])}).</p> | |
| 45 | * | |
| 46 | * <p>Also guarantees that, consistent with the {@link Reader} class, all read operations are | |
| 47 | * by default defined in terms of the (declared abstract) {@link #read(byte[], int, int)} method.</p> | |
| 48 | * | |
| 49 | * @see DirectReader | |
| 50 | * @see DirectOutputStream | |
| 51 | * @see DirectWriter | |
| 52 | */ | |
| 53 | public abstract class DirectInputStream extends InputStream { | |
| 54 | ||
| 55 | protected static final int DEFAULT_BUFFER_SIZE = 1024; | |
| 56 | ||
| 57 | /** Delegate to the more general {@link #read(byte[], int, int)} method */ | |
| 58 | 0 | @Override public int read() throws IOException { |
| 59 | 0 | byte[] bbuf = new byte[1]; |
| 60 | 0 | int readResult = read(bbuf, 0, 1); |
| 61 | 0 | if (readResult == -1) { return readResult; } |
| 62 | 0 | else if (readResult == 1) { return bbuf[0]; } |
| 63 | 0 | else { throw new IOException("Unexpected read result: " + readResult); } |
| 64 | } | |
| 65 | ||
| 66 | /** Delegate to the more general {@link #read(byte[], int, int)} method */ | |
| 67 | 0 | @Override public int read(byte[] bbuf) throws IOException { return read(bbuf, 0, bbuf.length); } |
| 68 | ||
| 69 | /** Subclasses are, at a minimum, required to implement this method. */ | |
| 70 | @Override public abstract int read(byte[] bbuf, int offset, int bytes) throws IOException; | |
| 71 | ||
| 72 | /** | |
| 73 | * Read some number of bytes from this stream, sending them to the provided {@code OutputStream}. | |
| 74 | * The default implementation invokes {@link #read(OutputStream, int, int)} with the minimum of | |
| 75 | * {@code bytes} and {@link #DEFAULT_BUFFER_SIZE}. Subclasses that know the size of this stream's | |
| 76 | * remaining contents, or that do not rely on a buffer in {@link #read(OutputStream, int, byte[])}, | |
| 77 | * should override this method. | |
| 78 | * | |
| 79 | * @param out A stream to be written to | |
| 80 | * @param bytes The number of bytes to read | |
| 81 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 82 | * @throws IOException If an error occurs during reading or writing | |
| 83 | */ | |
| 84 | 0 | public int read(OutputStream out, int bytes) throws IOException { |
| 85 | 0 | return read(out, bytes, (bytes < DEFAULT_BUFFER_SIZE) ? bytes : DEFAULT_BUFFER_SIZE); |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Read some number of bytes from this stream, sending them to the provided {@code OutputStream}. | |
| 90 | * The default implementation invokes {@link #read(OutputStream, int, byte[])} with a newly-allocated array | |
| 91 | * of the given size. Subclasses that do not rely on a buffer in {@link #read(OutputStream, int, byte[])} | |
| 92 | * should override this method. | |
| 93 | * | |
| 94 | * @param out A stream to be written to | |
| 95 | * @param bytes The number of bytes to read | |
| 96 | * @param bufferSize The size of buffer to use (if necessary). Smaller values may reduce the amount of | |
| 97 | * memory required; larger values may increase performance of large streams | |
| 98 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 99 | * @throws IOException If an error occurs during reading or writing | |
| 100 | * @throws IllegalArgumentException If {@code bufferSize <= 0} | |
| 101 | */ | |
| 102 | 0 | public int read(OutputStream out, int bytes, int bufferSize) throws IOException { |
| 103 | 0 | return read(out, bytes, new byte[bufferSize]); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Read some number of bytes from this stream, sending them to the provided {@code OutputStream}. | |
| 108 | * The given buffer is useful in repeated {@code read} invocations to avoid unnecessary | |
| 109 | * memory allocation. The default implementation repeatedly fills the given buffer via a | |
| 110 | * {@link InputStream#read(byte[], int, int)} operation, then writes it via | |
| 111 | * {@link OutputStream#write(byte[], int, int)}. Subclasses that do not require an external buffer | |
| 112 | * should override this method. | |
| 113 | * | |
| 114 | * @param out A stream to be written to | |
| 115 | * @param bytes The number of bytes to read | |
| 116 | * @param buffer A buffer used to copy bytes from this stream to the output stream. Note that this is only | |
| 117 | * used to avoid unnecessary memory allocation. No assumptions are made about the buffer's | |
| 118 | * contents (which may be overwritten), and no assumptions should be made about the contents | |
| 119 | * of the buffer after the method invocation. | |
| 120 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 121 | * @throws IOException If an error occurs during reading or writing | |
| 122 | * @throws IllegalArgumentException If {@code buffer} has size {@code 0} | |
| 123 | */ | |
| 124 | 0 | public int read(OutputStream out, int bytes, byte[] buffer) throws IOException { |
| 125 | 0 | return IOUtil.doWriteFromInputStream(this, out, bytes, buffer); |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * Read the full contents of this stream, sending the bytes to the provided {@code OutputStream}. | |
| 130 | * The method will block until an end-of-file is reached. The default implementation invokes | |
| 131 | * {@link #readAll(OutputStream, int)} with {@link #DEFAULT_BUFFER_SIZE}. Subclasses that know the | |
| 132 | * size of this stream's remaining contents, or that do not rely on a buffer in | |
| 133 | * {@link #readAll(OutputStream, byte[])}, should override this method. | |
| 134 | * | |
| 135 | * @param out A stream to be written to | |
| 136 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 137 | * (or, if the number is too large, {@code Integer.MAX_VALUE}) | |
| 138 | * @throws IOException If an error occurs during reading or writing | |
| 139 | */ | |
| 140 | 45 | public int readAll(OutputStream out) throws IOException { return readAll(out, DEFAULT_BUFFER_SIZE); } |
| 141 | ||
| 142 | /** | |
| 143 | * Read the full contents of this stream, sending the bytes to the provided {@code OutputStream}. | |
| 144 | * The method will block until an end-of-file is reached. The default implementation invokes | |
| 145 | * {@link #readAll(OutputStream, byte[])} with a newly-allocated array of the given size. Subclasses | |
| 146 | * that do not rely on a buffer in {@link #readAll(OutputStream, byte[])} should override this method. | |
| 147 | * | |
| 148 | * @param out A stream to be written to | |
| 149 | * @param bufferSize The size of buffer to use (if necessary). Smaller values may reduce the amount of | |
| 150 | * memory required; larger values may increase performance of large streams | |
| 151 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 152 | * (or, if the number is too large, {@code Integer.MAX_VALUE}) | |
| 153 | * @throws IOException If an error occurs during reading or writing | |
| 154 | * @throws IllegalArgumentException If {@code bufferSize <= 0} | |
| 155 | */ | |
| 156 | 45 | public int readAll(OutputStream out, int bufferSize) throws IOException { |
| 157 | 45 | return readAll(out, new byte[bufferSize]); |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Read the full contents of this stream, sending the bytes to the provided {@code OutputStream}. | |
| 162 | * The given buffer is useful in repeated {@code readAll} invocations to avoid unnecessary | |
| 163 | * memory allocation. The method will block until an end-of-file is reached. The default | |
| 164 | * implementation repeatedly fills the given buffer via a {@link InputStream#read(byte[])} operation, | |
| 165 | * then writes it via {@link OutputStream#write(byte[])}. Subclasses that do not require an external buffer | |
| 166 | * should override this method. | |
| 167 | * | |
| 168 | * @param out A stream to be written to | |
| 169 | * @param buffer A buffer used to copy bytes from this stream to the output stream. Note that this is only | |
| 170 | * used to avoid unnecessary memory allocation. No assumptions are made about the buffer's | |
| 171 | * contents (which may be overwritten), and no assumptions should be made about the contents | |
| 172 | * of the buffer after the method invocation. | |
| 173 | * @return {@code -1} if this stream is at the end of file; otherwise, the number of bytes read | |
| 174 | * (or, if the number is too large, {@code Integer.MAX_VALUE}) | |
| 175 | * @throws IOException If an error occurs during reading or writing | |
| 176 | * @throws IllegalArgumentException If {@code buffer} has size {@code 0} | |
| 177 | */ | |
| 178 | 45 | public int readAll(OutputStream out, byte[] buffer) throws IOException { |
| 179 | 45 | return IOUtil.doCopyInputStream(this, out, buffer); |
| 180 | } | |
| 181 | ||
| 182 | } |
|
||||||||||