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