| /* |
| * Copyright 2020 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package androidx.exifinterface.media; |
| |
| import static androidx.exifinterface.media.ExifInterface.IMAGE_TYPE_JPEG; |
| import static androidx.exifinterface.media.ExifInterface.IMAGE_TYPE_PNG; |
| import static androidx.exifinterface.media.ExifInterface.IMAGE_TYPE_WEBP; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| |
| class ExifInterfaceUtils { |
| private static final String TAG = "ExifInterfaceUtils"; |
| |
| private ExifInterfaceUtils() { |
| // Prevent instantiation |
| } |
| /** |
| * Copies all of the bytes from {@code in} to {@code out}. Neither stream is closed. |
| * Returns the total number of bytes transferred. |
| */ |
| static int copy(InputStream in, OutputStream out) throws IOException { |
| int total = 0; |
| byte[] buffer = new byte[8192]; |
| int c; |
| while ((c = in.read(buffer)) != -1) { |
| total += c; |
| out.write(buffer, 0, c); |
| } |
| return total; |
| } |
| |
| /** |
| * Copies the given number of the bytes from {@code in} to {@code out}. Neither stream is |
| * closed. |
| */ |
| static void copy(InputStream in, OutputStream out, int numBytes) throws IOException { |
| int remainder = numBytes; |
| byte[] buffer = new byte[8192]; |
| while (remainder > 0) { |
| int bytesToRead = Math.min(remainder, 8192); |
| int bytesRead = in.read(buffer, 0, bytesToRead); |
| if (bytesRead != bytesToRead) { |
| throw new IOException("Failed to copy the given amount of bytes from the input" |
| + "stream to the output stream."); |
| } |
| remainder -= bytesRead; |
| out.write(buffer, 0, bytesRead); |
| } |
| } |
| |
| /** |
| * Convert given int[] to long[]. If long[] is given, just return it. |
| * Return null for other types of input. |
| */ |
| static long[] convertToLongArray(Object inputObj) { |
| if (inputObj instanceof int[]) { |
| int[] input = (int[]) inputObj; |
| long[] result = new long[input.length]; |
| for (int i = 0; i < input.length; i++) { |
| result[i] = input[i]; |
| } |
| return result; |
| } else if (inputObj instanceof long[]) { |
| return (long[]) inputObj; |
| } |
| return null; |
| } |
| |
| static boolean startsWith(byte[] cur, byte[] val) { |
| if (cur == null || val == null) { |
| return false; |
| } |
| if (cur.length < val.length) { |
| return false; |
| } |
| for (int i = 0; i < val.length; i++) { |
| if (cur[i] != val[i]) { |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| static boolean isSupportedFormatForSavingAttributes(int mimeType) { |
| if (mimeType == IMAGE_TYPE_JPEG || mimeType == IMAGE_TYPE_PNG |
| || mimeType == IMAGE_TYPE_WEBP) { |
| return true; |
| } |
| return false; |
| } |
| |
| static String byteArrayToHexString(byte[] bytes) { |
| StringBuilder sb = new StringBuilder(bytes.length * 2); |
| for (int i = 0; i < bytes.length; i++) { |
| sb.append(String.format("%02x", bytes[i])); |
| } |
| return sb.toString(); |
| } |
| |
| static long parseSubSeconds(String subSec) { |
| try { |
| final int len = Math.min(subSec.length(), 3); |
| long sub = Long.parseLong(subSec.substring(0, len)); |
| for (int i = len; i < 3; i++) { |
| sub *= 10; |
| } |
| return sub; |
| } catch (NumberFormatException e) { |
| // Ignored |
| } |
| return 0L; |
| } |
| } |