Bug 1697844 - Remove unused IOUtils. r=esawin

Differential Revision: https://phabricator.services.mozilla.com/D108069
This commit is contained in:
Agi Sferro 2021-03-24 20:06:26 +00:00
Родитель 8370647485
Коммит 14735aa95a
3 изменённых файлов: 13 добавлений и 127 удалений

Просмотреть файл

@ -17,9 +17,11 @@ import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -65,7 +67,11 @@ public class DebugConfig {
} catch (final YAMLException e) {
throw new ConfigException(e.getMessage());
} finally {
IOUtils.safeStreamClose(fileInputStream);
try {
if (fileInputStream != null) {
((Closeable) fileInputStream).close();
}
} catch (final IOException e) { }
}
}

Просмотреть файл

@ -6,6 +6,7 @@ package org.mozilla.gecko.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
@ -51,7 +52,11 @@ public final class INIParser extends INISection {
} catch (final IOException e) {
e.printStackTrace();
} finally {
IOUtils.safeStreamClose(writer);
try {
if (writer != null) {
((Closeable) writer).close();
}
} catch (final IOException e) { }
}
}

Просмотреть файл

@ -1,125 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.util;
import android.util.Log;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Static helper class containing useful methods for manipulating IO objects.
*/
public class IOUtils {
private static final String LOGTAG = "GeckoIOUtils";
/**
* Represents the result of consuming an input stream, holding the returned data as well
* as the length of the data returned.
* The byte[] is not guaranteed to be trimmed to the size of the data acquired from the stream:
* hence the need for the length field. This strategy avoids the need to copy the data into a
* trimmed buffer after consumption.
*/
public static class ConsumedInputStream {
public final int consumedLength;
// Only reassigned in getTruncatedData.
private byte[] mConsumedData;
public ConsumedInputStream(final int consumedLength, final byte[] consumedData) {
this.consumedLength = consumedLength;
this.mConsumedData = consumedData;
}
/**
* Get the data trimmed to the length of the actual payload read, caching the result.
*/
public byte[] getTruncatedData() {
if (mConsumedData.length == consumedLength) {
return mConsumedData;
}
mConsumedData = truncateBytes(mConsumedData, consumedLength);
return mConsumedData;
}
public byte[] getData() {
return mConsumedData;
}
}
/**
* Fully read an InputStream into a byte array.
* @param iStream the InputStream to consume.
* @param bufferSize The initial size of the buffer to allocate. It will be grown as
* needed, but if the caller knows something about the InputStream then
* passing a good value here can improve performance.
*/
public static ConsumedInputStream readFully(final InputStream iStream, final int bufferSize) {
// Allocate a buffer to hold the raw data downloaded.
byte[] buffer = new byte[bufferSize];
// The offset of the start of the buffer's free space.
int bPointer = 0;
// The quantity of bytes the last call to read yielded.
int lastRead = 0;
try {
// Fully read the data into the buffer.
while (lastRead != -1) {
// Read as many bytes as are currently available into the buffer.
lastRead = iStream.read(buffer, bPointer, buffer.length - bPointer);
bPointer += lastRead;
// If buffer has overflowed, double its size and carry on.
if (bPointer > buffer.length) {
final int newBufferSize = bufferSize * 2;
final byte[] newBuffer = new byte[newBufferSize];
// Copy the contents of the old buffer into the new buffer.
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
buffer = newBuffer;
}
}
return new ConsumedInputStream(bPointer + 1, buffer);
} catch (final IOException e) {
Log.e(LOGTAG, "Error consuming input stream.", e);
} finally {
IOUtils.safeStreamClose(iStream);
}
return null;
}
/**
* Truncate a given byte[] to a given length. Returns a new byte[] with the first length many
* bytes of the input.
*/
public static byte[] truncateBytes(final byte[] bytes, final int length) {
final byte[] newBytes = new byte[length];
System.arraycopy(bytes, 0, newBytes, 0, length);
return newBytes;
}
public static void safeStreamClose(final Closeable stream) {
try {
if (stream != null)
stream.close();
} catch (final IOException e) { }
}
public static void copy(final InputStream in, final OutputStream out) throws IOException {
final byte[] buffer = new byte[4096];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
}