Skip to content

Commit

Permalink
chore: Remove dependency to apache commons lib (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Oct 21, 2023
1 parent 213c682 commit 6820bc8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,7 +93,7 @@ private static <T> T takeDeviceScreenshot(Class<T> 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");
}
Expand Down
31 changes: 29 additions & 2 deletions app/src/main/java/io/appium/uiautomator2/utils/StringHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6820bc8

Please sign in to comment.