Showing the changes to Source/Libraries/TSFJavaLibrary/src/org/gpa/tsf/util/InputStreamExtensions.java (1 of 915 files). Show all
Unified · Split
| @@ -0,0 +1,73 @@ | ||
| 1 | +//****************************************************************************************************** | |
| 2 | +// InputStreamExtensions.java - Gbtc | |
| 3 | +// | |
| 4 | +// Copyright � 2010, Grid Protection Alliance. All Rights Reserved. | |
| 5 | +// | |
| 6 | +// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | |
| 7 | +// the NOTICE file distributed with this work for additional information regarding copyright ownership. | |
| 8 | +// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | |
| 9 | +// not use this file except in compliance with the License. You may obtain a copy of the License at: | |
| 10 | +// | |
| 11 | +// http://www.opensource.org/licenses/eclipse-1.0.php | |
| 12 | +// | |
| 13 | +// Unless agreed to in writing, the subject software distributed under the License is distributed on an | |
| 14 | +// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | |
| 15 | +// License for the specific language governing permissions and limitations. | |
| 16 | +// | |
| 17 | +// Code Modification History: | |
| 18 | +// ---------------------------------------------------------------------------------------------------- | |
| 19 | +// 04/16/2012 - Stephen C. Wills | |
| 20 | +// Generated original version of source code. | |
| 21 | +// | |
| 22 | +//****************************************************************************************************** | |
| 23 | + | |
| 24 | +package org.gpa.tsf.util; | |
| 25 | + | |
| 26 | +import java.io.BufferedInputStream; | |
| 27 | +import java.io.IOException; | |
| 28 | +import java.io.InputStream; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Utility class that provides additional functionality for input streams. | |
| 32 | + * | |
| 33 | + * @author Stephen C. Wills | |
| 34 | + */ | |
| 35 | +public class InputStreamExtensions | |
| 36 | +{ | |
| 37 | + /** | |
| 38 | + * This read method works approximately the same as the | |
| 39 | + * {@link BufferedInputStream#read(byte[], int, int)} method. | |
| 40 | + * The main difference is that this method will block until the | |
| 41 | + * requested amount of data is read from the input stream or the | |
| 42 | + * end of the stream has been reached, whereas BufferedInputStream | |
| 43 | + * will try to avoid blocking by checking the {@code available()} | |
| 44 | + * method of the underlying stream. | |
| 45 | + * | |
| 46 | + * @param stream source of the data | |
| 47 | + * @param buffer destination buffer | |
| 48 | + * @param offset offset at which to start storing bytes | |
| 49 | + * @param length requested number of bytes | |
| 50 | + * @return the number of bytes read from the stream, or -1 if the end of the stream has been reached | |
| 51 | + * @throws IOException if this input stream has been closed by invoking its | |
| 52 | + * {@link InputStream#close()} method, or an I/O error occurs | |
| 53 | + * @see java.io.BufferedInputStream | |
| 54 | + */ | |
| 55 | + public static int read(InputStream stream, byte[] buffer, int offset, int length) throws IOException | |
| 56 | + { | |
| 57 | + int index = offset; | |
| 58 | + int readAmount = 0; | |
| 59 | + int totalReadAmount = 0; | |
| 60 | + | |
| 61 | + while (readAmount >= 0 && totalReadAmount < length) | |
| 62 | + { | |
| 63 | + readAmount = stream.read(buffer, index, length - readAmount); | |
| 64 | + index += readAmount; | |
| 65 | + totalReadAmount += readAmount; | |
| 66 | + } | |
| 67 | + | |
| 68 | + if (readAmount < 0 && totalReadAmount == 0) | |
| 69 | + totalReadAmount = -1; | |
| 70 | + | |
| 71 | + return totalReadAmount; | |
| 72 | + } | |
| 73 | +} | |