18e897890b080c8919b1d25cfbab90b61e0e84df4chrismair/* 28e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Copyright 2008 the original author or authors. 3e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * 48e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 58e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * you may not use this file except in compliance with the License. 68e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * You may obtain a copy of the License at 7e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * 88e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * http://www.apache.org/licenses/LICENSE-2.0 9e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * 108e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Unless required by applicable law or agreed to in writing, software 118e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * distributed under the License is distributed on an "AS IS" BASIS, 128e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * See the License for the specific language governing permissions and 148e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * limitations under the License. 158e897890b080c8919b1d25cfbab90b61e0e84df4chrismair */ 16e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismairpackage org.mockftpserver.core.util; 17e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair 18e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismairimport java.io.ByteArrayOutputStream; 19e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismairimport java.io.IOException; 20e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismairimport java.io.InputStream; 218e897890b080c8919b1d25cfbab90b61e0e84df4chrismair 228e897890b080c8919b1d25cfbab90b61e0e84df4chrismair/** 238e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Contains static I/O-related utility methods. 24e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * 258e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * @author Chris Mair 262a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$ 278e897890b080c8919b1d25cfbab90b61e0e84df4chrismair */ 28e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismairpublic class IoUtil { 298e897890b080c8919b1d25cfbab90b61e0e84df4chrismair 308e897890b080c8919b1d25cfbab90b61e0e84df4chrismair /** 318e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Read the contents of the InputStream and return as a byte[]. 32e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * 33e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * @param input - the InputStream to read 348e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * @return the contents of the InputStream as a byte[] 35e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * @throws AssertFailedException - if the InputStream is null 36e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair * @throws java.io.IOException - if an error occurs reading the bytes 378e897890b080c8919b1d25cfbab90b61e0e84df4chrismair */ 38e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair public static byte[] readBytes(InputStream input) throws IOException { 39e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair Assert.notNull(input, "input"); 40e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 418e897890b080c8919b1d25cfbab90b61e0e84df4chrismair 428e897890b080c8919b1d25cfbab90b61e0e84df4chrismair try { 438e897890b080c8919b1d25cfbab90b61e0e84df4chrismair while (true) { 44e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair int b = input.read(); 458e897890b080c8919b1d25cfbab90b61e0e84df4chrismair if (b == -1) { 46e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair break; 478e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 48e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair outBytes.write(b); 498e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 508e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 518e897890b080c8919b1d25cfbab90b61e0e84df4chrismair finally { 52e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair input.close(); 538e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 54e08928ac993d1e9d907c99f5c182ddb15b711f4cchrismair return outBytes.toByteArray(); 558e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 568e897890b080c8919b1d25cfbab90b61e0e84df4chrismair 578e897890b080c8919b1d25cfbab90b61e0e84df4chrismair /** 588e897890b080c8919b1d25cfbab90b61e0e84df4chrismair * Private constructor to prevent instantiation. All members are static. 598e897890b080c8919b1d25cfbab90b61e0e84df4chrismair */ 608e897890b080c8919b1d25cfbab90b61e0e84df4chrismair private IoUtil() { 618e897890b080c8919b1d25cfbab90b61e0e84df4chrismair } 628e897890b080c8919b1d25cfbab90b61e0e84df4chrismair 638e897890b080c8919b1d25cfbab90b61e0e84df4chrismair} 64