| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.BufferedInputStream; |
| 36 | | | import java.io.ByteArrayInputStream; |
| 37 | | | import java.io.ByteArrayOutputStream; |
| 38 | | | import java.io.File; |
| 39 | | | import java.io.FileInputStream; |
| 40 | | | import java.io.FileOutputStream; |
| 41 | | | import java.io.IOException; |
| 42 | | | import java.io.InputStream; |
| 43 | | | import java.io.OutputStream; |
| 44 | | | import java.io.RandomAccessFile; |
| 45 | | | import java.io.Reader; |
| 46 | | | import java.io.Writer; |
| 47 | | | import java.nio.ByteBuffer; |
| 48 | | | import java.nio.channels.Channel; |
| 49 | | | import java.nio.channels.ReadableByteChannel; |
| 50 | | | import java.util.Arrays; |
| 51 | | | import java.util.Random; |
| 52 | | | |
| 53 | | | import junit.framework.TestCase; |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | {@link } |
| 58 | | | |
| 59 | | | @author |
| 60 | | | |
| 61 | 100 | | public class IoUtilTest |
| 62 | | | extends TestCase |
| 63 | | | { |
| 64 | 100 | | private static final Random RANDOM_GENERATOR = new Random(); |
| 65 | | | |
| 66 | | | private static final int GAP_SIZE = 10 * 1024; |
| 67 | | | |
| 68 | | | |
| 69 | | | {@link } |
| 70 | | | @throws |
| 71 | | | |
| 72 | | | public void testReadFully () |
| 73 | | | throws IOException |
| 74 | | | { |
| 75 | 100 | | read1kBytes(); |
| 76 | 100 | | read0Bytes(); |
| 77 | 100 | | readAhead(); |
| 78 | 100 | | underread(); |
| 79 | 100 | | } |
| 80 | | | |
| 81 | | | |
| 82 | | | {@link } |
| 83 | | | |
| 84 | | | public void testSafeCloseInputStream () |
| 85 | | | { |
| 86 | 100 | | final InputStream in = new ByteArrayInputStream(new byte[0]) |
| 87 | 100 | (1) | { |
| 88 | | | public void close () |
| 89 | | | throws IOException |
| 90 | | | { |
| 91 | 100 | | throw new IOException(); |
| 92 | | | } |
| 93 | | | }; |
| 94 | | | |
| 95 | 100 | | IoUtil.close(in); |
| 96 | 100 | | IoUtil.close(in); |
| 97 | 100 | | IoUtil.close((InputStream) null); |
| 98 | 100 | | } |
| 99 | | | |
| 100 | | | |
| 101 | | | {@link } |
| 102 | | | |
| 103 | | | public void testSafeCloseOutputStream () |
| 104 | | | { |
| 105 | 100 | | final OutputStream out = new ByteArrayOutputStream() |
| 106 | 100 | (2) | { |
| 107 | | | public void close () |
| 108 | | | throws IOException |
| 109 | | | { |
| 110 | 100 | | throw new IOException(); |
| 111 | | | } |
| 112 | | | }; |
| 113 | | | |
| 114 | 100 | | IoUtil.close(out); |
| 115 | 100 | | IoUtil.close(out); |
| 116 | 100 | | IoUtil.close((OutputStream) null); |
| 117 | 100 | | } |
| 118 | | | |
| 119 | | | |
| 120 | | | {@link } |
| 121 | | | |
| 122 | | | public void testSafeCloseReader () |
| 123 | | | { |
| 124 | 100 | | final Reader reader = new Reader() |
| 125 | 100 | (3) | { |
| 126 | | | public void close () |
| 127 | | | throws IOException |
| 128 | | | { |
| 129 | 100 | | throw new IOException(); |
| 130 | | | } |
| 131 | | | |
| 132 | | | public int read (char[] cbuf, int off, int len) |
| 133 | | | { |
| 134 | 0 | | return 0; |
| 135 | | | } |
| 136 | | | }; |
| 137 | | | |
| 138 | 100 | | IoUtil.close(reader); |
| 139 | 100 | | IoUtil.close(reader); |
| 140 | 100 | | IoUtil.close((Reader) null); |
| 141 | 100 | | } |
| 142 | | | |
| 143 | | | |
| 144 | | | {@link } |
| 145 | | | |
| 146 | | | public void testSafeCloseWriter () |
| 147 | | | { |
| 148 | 100 | | final Writer writer = new Writer() |
| 149 | 100 | (4) | { |
| 150 | | | public void close () |
| 151 | | | throws IOException |
| 152 | | | { |
| 153 | 100 | | throw new IOException(); |
| 154 | | | } |
| 155 | | | |
| 156 | | | public void flush () |
| 157 | | | { |
| 158 | | | |
| 159 | 0 | | } |
| 160 | | | |
| 161 | | | public void write (char[] cbuf, int off, int len) |
| 162 | | | { |
| 163 | | | |
| 164 | 0 | | } |
| 165 | | | }; |
| 166 | | | |
| 167 | 100 | | IoUtil.close(writer); |
| 168 | 100 | | IoUtil.close(writer); |
| 169 | 100 | | IoUtil.close((Writer) null); |
| 170 | 100 | | } |
| 171 | | | |
| 172 | | | |
| 173 | | | {@link } |
| 174 | | | @throws |
| 175 | | | |
| 176 | | | public void testSafeCloseRandomAccessFile () |
| 177 | | | throws Exception |
| 178 | | | { |
| 179 | 100 | | final File tmp = File.createTempFile("tmp", "tmp"); |
| 180 | 100 | | tmp.deleteOnExit(); |
| 181 | 100 | | final RandomAccessFile file = new RandomAccessFile(tmp, "r") |
| 182 | 100 | (5) | { |
| 183 | | | public void close () |
| 184 | | | throws IOException |
| 185 | | | { |
| 186 | 100 | | throw new IOException(); |
| 187 | | | } |
| 188 | | | }; |
| 189 | | | |
| 190 | 100 | | IoUtil.close(file); |
| 191 | 100 | | IoUtil.close(file); |
| 192 | 100 | | IoUtil.close((RandomAccessFile) null); |
| 193 | 100 | | } |
| 194 | | | |
| 195 | | | |
| 196 | | | {@link } |
| 197 | | | @throws |
| 198 | | | |
| 199 | | | public void testSafeCloseChannel () |
| 200 | | | throws Exception |
| 201 | | | { |
| 202 | 100 | | final Channel channel = new ChannelImpl(); |
| 203 | 100 | | IoUtil.close(channel); |
| 204 | 100 | | IoUtil.close(channel); |
| 205 | 100 | | IoUtil.close((Channel) null); |
| 206 | 100 | | } |
| 207 | | | |
| 208 | | | |
| 209 | | (6)(7)(8) | public void testSkip() throws Exception { |
| 210 | 100 | | final File tmp = File.createTempFile("fawkez", "tmp"); |
| 211 | 100 | (9)(10)(11) | FileOutputStream out = new FileOutputStream(tmp); |
| 212 | 100 | | out.write(1); |
| 213 | 100 | | out.write(new byte[GAP_SIZE]); |
| 214 | 100 | (12) | out.write(2); |
| 215 | 100 | | out.close(); |
| 216 | 100 | (13)(14) | final InputStream in = new BufferedInputStream(new FileInputStream((tmp))); |
| 217 | 100 | (15) | int first = in.read(); |
| 218 | 100 | | IoUtil.skip(in, GAP_SIZE); |
| 219 | 100 | (16) | int second = in.read(); |
| 220 | 100 | (17)(18) | assertEquals(3, first + second); |
| 221 | | | |
| 222 | 100 | | } |
| 223 | | | |
| 224 | | | private void read1kBytes () |
| 225 | | | throws IOException |
| 226 | | | { |
| 227 | 100 | | final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE]; |
| 228 | 100 | | final InputStream in = createRandomInputStream(inData); |
| 229 | 100 | | final byte[] outData = IoUtil.readFully(in, inData.length); |
| 230 | 100 | | assertTrue("bytes arrays should be equal", |
| 231 | | | Arrays.equals(inData, outData)); |
| 232 | 100 | | } |
| 233 | | | |
| 234 | | | private void read0Bytes () |
| 235 | | | throws IOException |
| 236 | | | { |
| 237 | 100 | | final byte[] inData = new byte[0]; |
| 238 | 100 | | final InputStream in = createRandomInputStream(inData); |
| 239 | 100 | | final byte[] outData = IoUtil.readFully(in, inData.length); |
| 240 | 100 | | assertTrue("bytes arrays should be equal", |
| 241 | | | Arrays.equals(inData, outData)); |
| 242 | 100 | | } |
| 243 | | | |
| 244 | | | private void readAhead () |
| 245 | | | { |
| 246 | 0 | | final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE]; |
| 247 | 0 | | final InputStream in = createRandomInputStream(inData); |
| 248 | | | try |
| 249 | | | { |
| 250 | 0 | | IoUtil.readFully(in, inData.length + 1); |
| 251 | 0 | | fail("Expected IOException for reading more bytes than available"); |
| 252 | | | } |
| 253 | 100 | | catch (IOException expected) |
| 254 | | | { |
| 255 | 100 | | assertNotNull("Expected a real exception.", expected); |
| 256 | 0 | | } |
| 257 | 100 | | } |
| 258 | | | private void underread () |
| 259 | | | throws IOException |
| 260 | | | { |
| 261 | 100 | | final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE]; |
| 262 | 100 | | final InputStream in = createRandomInputStream(inData); |
| 263 | 100 | | final byte[] outData = IoUtil.readFully(in, inData.length - 1); |
| 264 | 100 | | final byte[] strippedInData = new byte[inData.length - 1]; |
| 265 | 100 | | System.arraycopy(inData, 0, strippedInData, 0, inData.length - 1); |
| 266 | 100 | | assertTrue("bytes arrays should be equal except the last one", |
| 267 | | | Arrays.equals(strippedInData, outData)); |
| 268 | 100 | | } |
| 269 | | | |
| 270 | | | private InputStream createRandomInputStream (byte[] inData) |
| 271 | | | { |
| 272 | 100 | | RANDOM_GENERATOR.nextBytes(inData); |
| 273 | 100 | | final InputStream in = new ByteArrayInputStream(inData); |
| 274 | 100 | (19)(20) | return in; |
| 275 | | | } |
| 276 | | | |
| 277 | 100 | | private static final class ChannelImpl |
| 278 | | | implements ReadableByteChannel |
| 279 | | | { |
| 280 | 100 | | private boolean mIsOpen = true; |
| 281 | | | |
| 282 | | | {@inheritDoc} |
| 283 | | | public int read (ByteBuffer dst) |
| 284 | | | { |
| 285 | 0 | | return 0; |
| 286 | | | } |
| 287 | | | |
| 288 | | | {@inheritDoc} |
| 289 | | | public void close () |
| 290 | | | throws IOException |
| 291 | | | { |
| 292 | 100 | | if (mIsOpen) |
| 293 | | | { |
| 294 | 100 | | mIsOpen = false; |
| 295 | | | } |
| 296 | | | else |
| 297 | | | { |
| 298 | 100 | | throw new IOException(); |
| 299 | | | } |
| 300 | 100 | | } |
| 301 | | | |
| 302 | | | {@inheritDoc} |
| 303 | | | public boolean isOpen () |
| 304 | | | { |
| 305 | 0 | | return mIsOpen; |
| 306 | | | } |
| 307 | | | } |
| 308 | | | |
| 309 | | | |
| 310 | | | } |