diff --git a/app/build.gradle b/app/build.gradle index 383ba374f..f7e436972 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,8 +93,6 @@ dependencies { implementation 'androidx.test:core:1.4.0' implementation 'androidx.test:runner:1.4.0' implementation 'com.google.code.gson:gson:2.10.1' - //noinspection GradleDependency - implementation 'commons-io:commons-io:2.6' implementation 'io.netty:netty-all:4.0.41.Final' implementation 'net.gcardone.junidecode:junidecode:0.4.1' // The next three dependencies are required for XPath search to work diff --git a/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoDumper.java b/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoDumper.java index 2818a0261..fe71157ac 100644 --- a/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoDumper.java +++ b/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoDumper.java @@ -27,7 +27,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.apache.commons.io.IOUtils; import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl; import org.eclipse.wst.xml.xpath2.api.Item; import org.eclipse.wst.xml.xpath2.api.ResultSequence; @@ -48,6 +47,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Iterator; import java.util.Objects; import java.util.Set; @@ -244,7 +244,7 @@ public String dumpToXml() { throw new UiAutomator2Exception(e); } try (InputStream xmlStream = toStream(false)) { - return IOUtils.toString(xmlStream, XML_ENCODING); + return StringHelpers.inputStreamToString(xmlStream, Charset.forName(XML_ENCODING)); } catch (IOException e) { throw new UiAutomator2Exception(e); } finally { diff --git a/app/src/main/java/io/appium/uiautomator2/utils/ScreenshotHelper.java b/app/src/main/java/io/appium/uiautomator2/utils/ScreenshotHelper.java index aeccebf39..260fc5294 100644 --- a/app/src/main/java/io/appium/uiautomator2/utils/ScreenshotHelper.java +++ b/app/src/main/java/io/appium/uiautomator2/utils/ScreenshotHelper.java @@ -28,8 +28,6 @@ import androidx.annotation.Nullable; -import org.apache.commons.io.IOUtils; - import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; @@ -95,7 +93,7 @@ private static T takeDeviceScreenshot(Class outputType) throws TakeScreen try { ParcelFileDescriptor pfd = automation.executeShellCommand("screencap -p"); try (InputStream is = new FileInputStream(pfd.getFileDescriptor())) { - byte[] pngBytes = IOUtils.toByteArray(is); + byte[] pngBytes = StringHelpers.inputStreamToByteArray(is); if (pngBytes.length <= PNG_MAGIC_LENGTH) { throw new IllegalStateException("screencap returned an invalid response"); } diff --git a/app/src/main/java/io/appium/uiautomator2/utils/StringHelpers.java b/app/src/main/java/io/appium/uiautomator2/utils/StringHelpers.java index 6504c034e..e715d8c8c 100644 --- a/app/src/main/java/io/appium/uiautomator2/utils/StringHelpers.java +++ b/app/src/main/java/io/appium/uiautomator2/utils/StringHelpers.java @@ -19,7 +19,15 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + public class StringHelpers { + private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; + private static final int EOF = -1; + public static String abbreviate(@Nullable String str, int maxLen) { if (str == null) { return null; @@ -68,8 +76,27 @@ public static String pluralize(long count, String singularWord) { @NonNull public static String pluralize(long count, String singularWord, boolean includeCount) { if (count == 1) { - return includeCount ? String.format("%s %s", count, singularWord) : String.valueOf(singularWord); + return includeCount + ? String.format("%s %s", count, singularWord) + : String.valueOf(singularWord); } - return includeCount ? String.format("%s %ss", count, singularWord) : String.format("%ss", singularWord); + return includeCount + ? String.format("%s %ss", count, singularWord) + : String.format("%ss", singularWord); + } + + public static byte[] inputStreamToByteArray(InputStream input) throws IOException { + try (ByteArrayOutputStream result = new ByteArrayOutputStream()) { + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + int length; + while ((length = input.read(buffer)) != EOF) { + result.write(buffer, 0, length); + } + return result.toByteArray(); + } + } + + public static String inputStreamToString(InputStream input, Charset encoding) throws IOException { + return new String(inputStreamToByteArray(input), encoding); } }