|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DirectOutputStream.java | 0% | 37.5% | 37.5% | 33.3% |
|
||||||||||||||
| 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 OutputStream} that supports writing directly from an {@code InputStream}. This class | |
| 41 | * provides default implementations defined in terms of {@code InputStream} and {@code OutputStream} | |
| 42 | * methods. Subclasses can override (at least) {@link #writeAll(InputStream, byte[])} and | |
| 43 | * {@link #write(InputStream, int, byte[])} to provide better implementations (by, for example, | |
| 44 | * not invoking {@link OutputStream#write(byte[])}).</p> | |
| 45 | * | |
| 46 | * <p>Also guarantees that, consistent with the {@link Writer} class, all write operations are | |
| 47 | * by default defined in terms of the (declared abstract) {@link #write(byte[], int, int)} method.</p> | |
| 48 | * | |
| 49 | * @see DirectWriter | |
| 50 | * @see DirectInputStream | |
| 51 | * @see DirectReader | |
| 52 | */ | |
| 53 | public abstract class DirectOutputStream extends OutputStream { | |
| 54 | ||
| 55 | protected static final int DEFAULT_BUFFER_SIZE = 1024; | |
| 56 | ||
| 57 | /** Delegate to the more general {@link #write(byte[], int, int)} method */ | |
| 58 | 0 | @Override public void write(int b) throws IOException { write(new byte[]{ (byte) b }, 0, 1); } |
| 59 | ||
| 60 | /** Delegate to the more general {@link #write(byte[], int, int)} method */ | |
| 61 | 0 | @Override public void write(byte[] bbuf) throws IOException { write(bbuf, 0, bbuf.length); } |
| 62 | ||
| 63 | /** Subclasses are, at a minimum, required to implement this method. */ | |
| 64 | @Override public abstract void write(byte[] bbuf, int offset, int bytes) throws IOException; | |
| 65 | ||
| 66 | /** | |
| 67 | * Write some number of bytes, using the provided {@code InputStream} as input. Fewer bytes | |
| 68 | * may be written if the input stream has fewer available. The default implementation | |
| 69 | * invokes {@link #write(InputStream, int, int)} with the minimum of {@code bytes} and | |
| 70 | * {@link #DEFAULT_BUFFER_SIZE}. Subclasses that do not rely on a buffer in | |
| 71 | * {@link #write(InputStream, int, byte[])} should override this method. | |
| 72 | * | |
| 73 | * @param in A stream to be read from | |
| 74 | * @param bytes The number of bytes to write | |
| 75 | * @return {@code -1} if the input stream is at the end of file; otherwise, the number of characters | |
| 76 | * written | |
| 77 | * @throws IOException If an error occurs during reading or writing | |
| 78 | */ | |
| 79 | 0 | public int write(InputStream in, int bytes) throws IOException { |
| 80 | 0 | return write(in, bytes, (bytes < DEFAULT_BUFFER_SIZE) ? bytes : DEFAULT_BUFFER_SIZE); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Write some number of bytes, using the provided {@code InputStream} as input. Fewer bytes | |
| 85 | * may be written if the input stream has fewer available. The default implementation | |
| 86 | * invokes {@link #write(InputStream, int, byte[])} with a newly-allocated array of the given size. | |
| 87 | * Subclasses that do not rely on a buffer in {@link #write(InputStream, int, byte[])} | |
| 88 | * should override this method. | |
| 89 | * | |
| 90 | * @param in A stream to be read from | |
| 91 | * @param bytes The number of bytes to write | |
| 92 | * @param bufferSize The size of buffer to use (if necessary). Smaller values may reduce the amount of | |
| 93 | * memory required; larger values may increase performance on large readers | |
| 94 | * @return {@code -1} if the input stream is at the end of file; otherwise, the number of characters | |
| 95 | * written | |
| 96 | * @throws IOException If an error occurs during reading or writing | |
| 97 | * @throws IllegalArgumentException If {@code bufferSize <= 0} | |
| 98 | */ | |
| 99 | 0 | public int write(InputStream in, int bytes, int bufferSize) throws IOException { |
| 100 | 0 | return write(in, bytes, new byte[bufferSize]); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Write some number of bytes, using the provided {@code InputStream} as input. Fewer bytes | |
| 105 | * may be written if the input stream has fewer available. The given buffer is useful | |
| 106 | * in repeated {@code write} invocations to avoid unnecessary memory allocation. The default | |
| 107 | * implementation repeatedly fills the given buffer via a | |
| 108 | * {@link InputStream#read(byte[], int, int)} operation, then writes it via | |
| 109 | * {@link OutputStream#write(byte[], int, int)}. Subclasses that do not require an external buffer | |
| 110 | * should override this method. | |
| 111 | * | |
| 112 | * @param in A stream to be read from | |
| 113 | * @param bytes The number of bytes to write | |
| 114 | * @param buffer A buffer used to copy bytes from the input stream to this stream. Note that this is only | |
| 115 | * used to avoid unnecessary memory allocation. No assumptions are made about the buffer's | |
| 116 | * contents (which may be overwritten), and no assumptions should be made about the contents | |
| 117 | * of the buffer after the method invocation. | |
| 118 | * @return {@code -1} if the input stream is at the end of file; otherwise, the number of characters | |
| 119 | * written | |
| 120 | * @throws IOException If an error occurs during reading or writing | |
| 121 | * @throws IllegalArgumentException If {@code buffer} has size {@code 0} | |
| 122 | */ | |
| 123 | 0 | public int write(InputStream in, int bytes, byte[] buffer) throws IOException { |
| 124 | 0 | return IOUtil.doWriteFromInputStream(in, this, bytes, buffer); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Write the full contents of the given {@code InputStream} to this stream. The method will block | |
| 129 | * until an end-of-file is reached. The default implementation invokes | |
| 130 | * {@link #writeAll(InputStream, int)} with {@link #DEFAULT_BUFFER_SIZE}. Subclasses that know the | |
| 131 | * size of this stream's remaining contents, or that do not rely on a buffer in | |
| 132 | * {@link #writeAll(InputStream, byte[])}, should override this method. | |
| 133 | * | |
| 134 | * @param in A stream to be read from | |
| 135 | * @return {@code -1} if the input stream is at the end of file; otherwise, the number of characters | |
| 136 | * written (or, if the number is too large, {@code Integer.MAX_VALUE}) | |
| 137 | * @throws IOException If an error occurs during reading or writing | |
| 138 | */ | |
| 139 | 5 | public int writeAll(InputStream in) throws IOException { |
| 140 | 5 | return writeAll(in, DEFAULT_BUFFER_SIZE); |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Write the full contents of the given {@code InputStream} to this stream. The method will block | |
| 145 | * until an end-of-file is reached. The default implementation invokes | |
| 146 | * {@link #writeAll(InputStream, byte[])} with a newly-allocated array of the given size. Subclasses | |
| 147 | * that do not rely on a buffer in {@link #writeAll(InputStream, byte[])} should override this method. | |
| 148 | * | |
| 149 | * @param in A stream to be read from | |
| 150 | * @param bufferSize The size of buffer to use (if necessary). Smaller values may reduce the amount of | |
| 151 | * memory required; larger values may increase performance on large readers | |
| 152 | * @return {@code -1} if the input stream is at the end of file; otherwise, the number of characters | |
| 153 | * written (or, if the number is too large, {@code Integer.MAX_VALUE}) | |
| 154 | * @throws IOException If an error occurs during reading or writing | |
| 155 | * @throws IllegalArgumentException If {@code bufferSize <= 0} | |
| 156 | */ | |
| 157 | 5 | public int writeAll(InputStream in, int bufferSize) throws IOException { |
| 158 | 5 | return writeAll(in, new byte[bufferSize]); |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Write the full contents of the given {@code InputStream} to this stream. The given buffer is useful | |
| 163 | * in repeated {@code writeAll} invocations to avoid unnecessary memory allocation. The method will | |
| 164 | * block until an end-of-file is reached. The default implementation repeatedly fills the given buffer | |
| 165 | * via a {@link InputStream#read(byte[])} operation, then writes it via {@link OutputStream#write(byte[])}. | |
| 166 | * Subclasses that do not require an external buffer should override this method. | |
| 167 | * | |
| 168 | * @param in A stream to be read from | |
| 169 | * @param buffer A buffer used to copy bytes from the input stream to this 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 the input stream is at the end of file; otherwise, the number of characters | |
| 174 | * written (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 | 5 | public int writeAll(InputStream in, byte[] buffer) throws IOException { |
| 179 | 5 | return IOUtil.doCopyInputStream(in, this, buffer); |
| 180 | } | |
| 181 | ||
| 182 | } |
|
||||||||||