Skip to content

Commit

Permalink
Remove Guava dependency
Browse files Browse the repository at this point in the history
This removes 700k on final apk.
I initially planed to use Proguard for that, but faced so many issues with Dagger+Proguard.
  • Loading branch information
Nilhcem committed May 8, 2013
1 parent af924b8 commit ae66402
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 17 deletions.
Binary file added libs/commons-io-2.4.jar
Binary file not shown.
Binary file removed libs/guava-14.0.1.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import butterknife.InjectView;
import butterknife.Views;

import com.google.common.net.InetAddresses;
import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.bus.event.CreatedHostEvent;
import com.nilhcem.hostseditor.core.BaseFragment;
import com.nilhcem.hostseditor.core.Host;
import com.nilhcem.hostseditor.util.InetAddresses;

public class AddEditHostFragment extends BaseFragment implements OnClickListener {
public static final String TAG = "AddHostFragment";
Expand Down
2 changes: 1 addition & 1 deletion src/com/nilhcem/hostseditor/core/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.os.Parcelable;
import android.text.TextUtils;

import com.google.common.net.InetAddresses;
import com.nilhcem.hostseditor.util.InetAddresses;

public class Host implements Parcelable {
public static final String STR_COMMENT = "#";
Expand Down
25 changes: 10 additions & 15 deletions src/com/nilhcem/hostseditor/core/HostsManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nilhcem.hostseditor.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -14,11 +13,12 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import android.content.Context;
import android.util.Log;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
Expand All @@ -27,6 +27,7 @@
public class HostsManager {
private static final String TAG = "HostsManager";

private static final String UTF_8 = "UTF-8";
private static final String HOSTS_FILE_NAME = "hosts";
private static final String HOSTS_FILE_PATH = "/system/etc/" + HOSTS_FILE_NAME;

Expand Down Expand Up @@ -55,26 +56,20 @@ public synchronized List<Host> getHosts(boolean forceRefresh) {
if (mHosts == null || forceRefresh) {
mHosts = Collections.synchronizedList(new ArrayList<Host>());

BufferedReader reader = null;
LineIterator it = null;
try {
reader = Files.newReader(new File(HOSTS_FILE_PATH), Charsets.UTF_8);

String line;
while ((line = reader.readLine()) != null) {
Host host = Host.fromString(line);
it = FileUtils.lineIterator(new File(HOSTS_FILE_PATH), UTF_8);
while (it.hasNext()) {
Host host = Host.fromString(it.nextLine());
if (host != null) {
mHosts.add(host);
}
}
} catch (IOException e) {
Log.e(TAG, "I/O error while opening hosts file", e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Log.e(TAG, "Error while closing reader", e);
if (it != null) {
LineIterator.closeQuietly(it);
}
}
}
Expand Down
194 changes: 194 additions & 0 deletions src/com/nilhcem/hostseditor/util/InetAddresses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Copyright (C) 2008 The Guava Authors
*
* NOTICE: THIS FILE WAS MODIFIED (removed all not required for the 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 com.nilhcem.hostseditor.util;

import java.net.InetAddress;
import java.nio.ByteBuffer;

/**
* Static utility methods pertaining to {@link InetAddress} instances.
*
*/
public final class InetAddresses {
private static final int IPV4_PART_COUNT = 4;
private static final int IPV6_PART_COUNT = 8;

private InetAddresses() {
}

/**
* Returns {@code true} if the supplied string is a valid IP string literal,
* {@code false} otherwise.
*
* @param ipString
* {@code String} to evaluated as an IP string literal
* @return {@code true} if the argument is a valid IP string literal
*/
public static boolean isInetAddress(String ipString) {
return ipStringToBytes(ipString) != null;
}

private static byte[] ipStringToBytes(String ipString) {
// Make a first pass to categorize the characters in this string.
boolean hasColon = false;
boolean hasDot = false;
for (int i = 0; i < ipString.length(); i++) {
char c = ipString.charAt(i);
if (c == '.') {
hasDot = true;
} else if (c == ':') {
if (hasDot) {
return null; // Colons must not appear after dots.
}
hasColon = true;
} else if (Character.digit(c, 16) == -1) {
return null; // Everything else must be a decimal or hex digit.
}
}

// Now decide which address family to parse.
if (hasColon) {
if (hasDot) {
ipString = convertDottedQuadToHex(ipString);
if (ipString == null) {
return null;
}
}
return textToNumericFormatV6(ipString);
} else if (hasDot) {
return textToNumericFormatV4(ipString);
}
return null;
}

private static byte[] textToNumericFormatV4(String ipString) {
String[] address = ipString.split("\\.", IPV4_PART_COUNT + 1);
if (address.length != IPV4_PART_COUNT) {
return null;
}

byte[] bytes = new byte[IPV4_PART_COUNT];
try {
for (int i = 0; i < bytes.length; i++) {
bytes[i] = parseOctet(address[i]);
}
} catch (NumberFormatException ex) {
return null;
}

return bytes;
}

private static byte[] textToNumericFormatV6(String ipString) {
// An address can have [2..8] colons, and N colons make N+1 parts.
String[] parts = ipString.split(":", IPV6_PART_COUNT + 2);
if (parts.length < 3 || parts.length > IPV6_PART_COUNT + 1) {
return null;
}

// Disregarding the endpoints, find "::" with nothing in between.
// This indicates that a run of zeroes has been skipped.
int skipIndex = -1;
for (int i = 1; i < parts.length - 1; i++) {
if (parts[i].length() == 0) {
if (skipIndex >= 0) {
return null; // Can't have more than one ::
}
skipIndex = i;
}
}

int partsHi; // Number of parts to copy from above/before the "::"
int partsLo; // Number of parts to copy from below/after the "::"
if (skipIndex >= 0) {
// If we found a "::", then check if it also covers the endpoints.
partsHi = skipIndex;
partsLo = parts.length - skipIndex - 1;
if (parts[0].length() == 0 && --partsHi != 0) {
return null; // ^: requires ^::
}
if (parts[parts.length - 1].length() == 0 && --partsLo != 0) {
return null; // :$ requires ::$
}
} else {
// Otherwise, allocate the entire address to partsHi. The endpoints
// could still be empty, but parseHextet() will check for that.
partsHi = parts.length;
partsLo = 0;
}

// If we found a ::, then we must have skipped at least one part.
// Otherwise, we must have exactly the right number of parts.
int partsSkipped = IPV6_PART_COUNT - (partsHi + partsLo);
if (!(skipIndex >= 0 ? partsSkipped >= 1 : partsSkipped == 0)) {
return null;
}

// Now parse the hextets into a byte array.
ByteBuffer rawBytes = ByteBuffer.allocate(2 * IPV6_PART_COUNT);
try {
for (int i = 0; i < partsHi; i++) {
rawBytes.putShort(parseHextet(parts[i]));
}
for (int i = 0; i < partsSkipped; i++) {
rawBytes.putShort((short) 0);
}
for (int i = partsLo; i > 0; i--) {
rawBytes.putShort(parseHextet(parts[parts.length - i]));
}
} catch (NumberFormatException ex) {
return null;
}
return rawBytes.array();
}

private static String convertDottedQuadToHex(String ipString) {
int lastColon = ipString.lastIndexOf(':');
String initialPart = ipString.substring(0, lastColon + 1);
String dottedQuad = ipString.substring(lastColon + 1);
byte[] quad = textToNumericFormatV4(dottedQuad);
if (quad == null) {
return null;
}
String penultimate = Integer.toHexString(((quad[0] & 0xff) << 8)
| (quad[1] & 0xff));
String ultimate = Integer.toHexString(((quad[2] & 0xff) << 8)
| (quad[3] & 0xff));
return initialPart + penultimate + ":" + ultimate;
}

private static byte parseOctet(String ipPart) {
// Note: we already verified that this string contains only hex digits.
int octet = Integer.parseInt(ipPart);
// Disallow leading zeroes, because no clear standard exists on
// whether these should be interpreted as decimal or octal.
if (octet > 255 || (ipPart.startsWith("0") && ipPart.length() > 1)) {
throw new NumberFormatException();
}
return (byte) octet;
}

private static short parseHextet(String ipPart) {
// Note: we already verified that this string contains only hex digits.
int hextet = Integer.parseInt(ipPart, 16);
if (hextet > 0xffff) {
throw new NumberFormatException();
}
return (short) hextet;
}
}

0 comments on commit ae66402

Please sign in to comment.