| 1 | /* |
| 2 | * Copyright 2002-2019 Drew Noakes and contributors |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * More information about this project is available at: |
| 17 | * |
| 18 | * https://drewnoakes.com/code/exif/ |
| 19 | * https://github.com/drewnoakes/metadata-extractor |
| 20 | */ |
| 21 | package com.drew.tools; |
| 22 | |
| 23 | import com.drew.lang.annotations.NotNull; |
| 24 | |
| 25 | import java.io.File; |
| 26 | import java.io.FileInputStream; |
| 27 | import java.io.FileOutputStream; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | /** |
| 31 | * A series of utility methods for working with the file system. The methods herein are used in unit testing. |
| 32 | * Use them in production code at your own risk! |
| 33 | * |
| 34 | * @author Drew Noakes https://drewnoakes.com |
| 35 | */ |
| 36 | public class FileUtil |
| 37 | { |
| 38 | /** |
| 39 | * Saves the contents of a <code>byte[]</code> to the specified {@link File}. |
| 40 | */ |
| 41 | public static void saveBytes(@NotNull File file, @NotNull byte[] bytes) throws IOException |
| 42 | { |
| 43 | FileOutputStream stream = null; |
| 44 | try { |
| 45 | stream = new FileOutputStream(file); |
| 46 | stream.write(bytes); |
| 47 | } finally { |
| 48 | if (stream != null) { |
| 49 | stream.close(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Reads the contents of a {@link File} into a <code>byte[]</code>. This relies upon {@link File#length()} |
| 56 | * returning the correct value, which may not be the case when using a network file system. However this method is |
| 57 | * intended for unit test support, in which case the files should be on the local volume. |
| 58 | */ |
| 59 | @NotNull |
| 60 | public static byte[] readBytes(@NotNull File file) throws IOException |
| 61 | { |
| 62 | int length = (int)file.length(); |
| 63 | // should only be zero if loading from a network or similar |
| 64 | assert (length != 0); |
| 65 | byte[] bytes = new byte[length]; |
| 66 | |
| 67 | int totalBytesRead = 0; |
| 68 | FileInputStream inputStream = null; |
| 69 | try { |
| 70 | inputStream = new FileInputStream(file); |
| 71 | while (totalBytesRead != length) { |
| 72 | int bytesRead = inputStream.read(bytes, totalBytesRead, length - totalBytesRead); |
| 73 | if (bytesRead == -1) { |
| 74 | break; |
| 75 | } |
| 76 | totalBytesRead += bytesRead; |
| 77 | } |
| 78 | } finally { |
| 79 | if (inputStream != null) { |
| 80 | inputStream.close(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return bytes; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Reads the contents of a {@link File} into a <code>byte[]</code>. This relies upon <code>File.length()</code> |
| 89 | * returning the correct value, which may not be the case when using a network file system. However this method is |
| 90 | * intended for unit test support, in which case the files should be on the local volume. |
| 91 | */ |
| 92 | @NotNull |
| 93 | public static byte[] readBytes(@NotNull String filePath) throws IOException |
| 94 | { |
| 95 | return readBytes(new File(filePath)); |
| 96 | } |
| 97 | } |