|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LinkedInputAndOutputStream.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.InputStream; | |
| 38 | import java.io.OutputStream; | |
| 39 | import java.io.IOException; | |
| 40 | ||
| 41 | /** | |
| 42 | * Coordinates reading and writing from the same source. Ensures | |
| 43 | * that the given OutputStream will never write past the location of the | |
| 44 | * InputStream, potentially corrupting its contents. When the OutputStream needs | |
| 45 | * to write more bytes than have been read, a buffer is used to | |
| 46 | * store the bytes in between. In order to take advantage of these | |
| 47 | * linking features, all interaction with the user-provided InputStream and OutputStream | |
| 48 | * must subsequently take place via the results of {@link #inputStream()} and | |
| 49 | * {@link #outputStream()}, respectively. | |
| 50 | */ | |
| 51 | public class LinkedInputAndOutputStream { | |
| 52 | ||
| 53 | private final DirectInputStream _linkedInputStream; | |
| 54 | private final DirectOutputStream _linkedOutputStream; | |
| 55 | private long _readIndex; // Must always be >= _writeIndex | |
| 56 | private boolean _eof; | |
| 57 | private long _writeIndex; | |
| 58 | ||
| 59 | /** | |
| 60 | * Link {@code in} and {@code out}. Assumes that {@code in} and {@code out} are newly created, or at | |
| 61 | * least that their underlying cursors point to the same place. Also assumes that no further | |
| 62 | * direct interaction with {@code in} or {@code out} will occur. | |
| 63 | */ | |
| 64 | 0 | public LinkedInputAndOutputStream(final InputStream in, final OutputStream out) { |
| 65 | 0 | _readIndex = 0; |
| 66 | 0 | _writeIndex = 0; |
| 67 | 0 | _eof = false; |
| 68 | 0 | final ExpandingByteBuffer buffer = new ExpandingByteBuffer(); |
| 69 | 0 | final InputStream fromBuffer = buffer.inputStream(); |
| 70 | 0 | final DirectOutputStream toBuffer = buffer.outputStream(); |
| 71 | ||
| 72 | 0 | _linkedInputStream = new DirectInputStream() { |
| 73 | ||
| 74 | 0 | public int read(byte[] bbuf, int offset, int bytes) throws IOException { |
| 75 | 0 | int read = 0; |
| 76 | ||
| 77 | 0 | if (!buffer.isEmpty()) { |
| 78 | 0 | int bufferReadResult = fromBuffer.read(bbuf, offset, bytes); |
| 79 | 0 | if (bufferReadResult < 0) { |
| 80 | 0 | throw new IllegalStateException("Unexpected negative result from ExpandingByteBuffer read"); |
| 81 | } | |
| 82 | 0 | else if (bufferReadResult > 0) { read += bufferReadResult; bytes -= bufferReadResult; } |
| 83 | } | |
| 84 | ||
| 85 | 0 | if (buffer.isEmpty() && bytes >= 0) { |
| 86 | 0 | int readResult = in.read(bbuf, offset + read, bytes); |
| 87 | 0 | if (readResult < 0) { |
| 88 | 0 | _eof = true; |
| 89 | 0 | return read > 0 ? read : readResult; |
| 90 | } | |
| 91 | else { | |
| 92 | 0 | read += readResult; |
| 93 | 0 | bytes -= readResult; |
| 94 | 0 | _readIndex += readResult; |
| 95 | } | |
| 96 | } | |
| 97 | 0 | return read; |
| 98 | } | |
| 99 | ||
| 100 | 0 | public void close() throws IOException { in.close(); } |
| 101 | ||
| 102 | 0 | public int available() throws IOException { return in.available(); } |
| 103 | }; | |
| 104 | ||
| 105 | 0 | _linkedOutputStream = new DirectOutputStream() { |
| 106 | 0 | public void write(byte[] bbuf, int offset, int bytes) throws IOException { |
| 107 | 0 | long newIndex = _writeIndex + bytes; |
| 108 | 0 | while (newIndex > _readIndex) { |
| 109 | // If we've reached the eof, we don't try to buffer anything | |
| 110 | 0 | if (_eof) { _readIndex = newIndex; } |
| 111 | else { | |
| 112 | 0 | int bufferWriteResult = toBuffer.write(in, (int) (newIndex - _readIndex)); |
| 113 | 0 | if (bufferWriteResult < 0) { _eof = true; } |
| 114 | 0 | else { _readIndex += bufferWriteResult; } |
| 115 | } | |
| 116 | } | |
| 117 | 0 | out.write(bbuf, offset, bytes); |
| 118 | 0 | _writeIndex = newIndex; |
| 119 | } | |
| 120 | ||
| 121 | 0 | public void close() throws IOException { out.close(); } |
| 122 | ||
| 123 | 0 | public void flush() throws IOException { out.flush(); } |
| 124 | }; | |
| 125 | } | |
| 126 | ||
| 127 | 0 | public DirectInputStream inputStream() { return _linkedInputStream; } |
| 128 | ||
| 129 | 0 | public DirectOutputStream outputStream() { return _linkedOutputStream; } |
| 130 | ||
| 131 | } |
|
||||||||||