This source file includes following definitions.
- hasSwitch
- getSwitchValue
- getSwitchValue
- appendSwitch
- appendSwitchWithValue
- appendSwitchesAndArguments
- isNativeImplementation
- isInitialized
- getInstance
- init
- initFromFile
- reset
- tokenizeQuotedAruments
- enableNativeProxy
- getJavaSwitchesOrNull
- setInstance
- readUtf8FileFully
- getCommandLineArguments
- hasSwitch
- getSwitchValue
- appendSwitch
- appendSwitchWithValue
- appendSwitchesAndArguments
- appendSwitchesInternal
- hasSwitch
- getSwitchValue
- appendSwitch
- appendSwitchWithValue
- appendSwitchesAndArguments
- isNativeImplementation
- nativeReset
- nativeHasSwitch
- nativeGetSwitchValue
- nativeAppendSwitch
- nativeAppendSwitchWithValue
- nativeAppendSwitchesAndArguments
package org.chromium.base;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;
public abstract class CommandLine {
public abstract boolean hasSwitch(String switchString);
public abstract String getSwitchValue(String switchString);
public String getSwitchValue(String switchString, String defaultValue) {
String value = getSwitchValue(switchString);
return TextUtils.isEmpty(value) ? defaultValue : value;
}
public abstract void appendSwitch(String switchString);
public abstract void appendSwitchWithValue(String switchString, String value);
public abstract void appendSwitchesAndArguments(String[] array);
public boolean isNativeImplementation() {
return false;
}
private static final AtomicReference<CommandLine> sCommandLine =
new AtomicReference<CommandLine>();
public static boolean isInitialized() {
return sCommandLine.get() != null;
}
public static CommandLine getInstance() {
CommandLine commandLine = sCommandLine.get();
assert commandLine != null;
return commandLine;
}
public static void init(String[] args) {
setInstance(new JavaCommandLine(args));
}
public static void initFromFile(String file) {
char[] buffer = readUtf8FileFully(file, 8 * 1024);
init(buffer == null ? null : tokenizeQuotedAruments(buffer));
}
public static void reset() {
setInstance(null);
}
public static String[] tokenizeQuotedAruments(char[] buffer) {
ArrayList<String> args = new ArrayList<String>();
StringBuilder arg = null;
final char noQuote = '\0';
final char singleQuote = '\'';
final char doubleQuote = '"';
char currentQuote = noQuote;
for (char c : buffer) {
if ((currentQuote == noQuote && (c == singleQuote || c == doubleQuote)) ||
c == currentQuote) {
if (arg != null && arg.length() > 0 && arg.charAt(arg.length() - 1) == '\\') {
arg.setCharAt(arg.length() - 1, c);
} else {
currentQuote = currentQuote == noQuote ? c : noQuote;
}
} else if (currentQuote == noQuote && Character.isWhitespace(c)) {
if (arg != null) {
args.add(arg.toString());
arg = null;
}
} else {
if (arg == null) arg = new StringBuilder();
arg.append(c);
}
}
if (arg != null) {
if (currentQuote != noQuote) {
Log.w(TAG, "Unterminated quoted string: " + arg);
}
args.add(arg.toString());
}
return args.toArray(new String[args.size()]);
}
private static final String TAG = "CommandLine";
private static final String SWITCH_PREFIX = "--";
private static final String SWITCH_TERMINATOR = SWITCH_PREFIX;
private static final String SWITCH_VALUE_SEPARATOR = "=";
public static void enableNativeProxy() {
sCommandLine.set(new NativeCommandLine());
}
public static String[] getJavaSwitchesOrNull() {
CommandLine commandLine = sCommandLine.get();
if (commandLine != null) {
assert !commandLine.isNativeImplementation();
return ((JavaCommandLine) commandLine).getCommandLineArguments();
}
return null;
}
private static void setInstance(CommandLine commandLine) {
CommandLine oldCommandLine = sCommandLine.getAndSet(commandLine);
if (oldCommandLine != null && oldCommandLine.isNativeImplementation()) {
nativeReset();
}
}
private static char[] readUtf8FileFully(String fileName, int sizeLimit) {
Reader reader = null;
File f = new File(fileName);
long fileLength = f.length();
if (fileLength == 0) {
return null;
}
if (fileLength > sizeLimit) {
Log.w(TAG, "File " + fileName + " length " + fileLength + " exceeds limit "
+ sizeLimit);
return null;
}
try {
char[] buffer = new char[(int) fileLength];
reader = new InputStreamReader(new FileInputStream(f), "UTF-8");
int charsRead = reader.read(buffer);
assert !reader.ready();
return charsRead < buffer.length ? Arrays.copyOfRange(buffer, 0, charsRead) : buffer;
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
} finally {
try {
if (reader != null) reader.close();
} catch (IOException e) {
Log.e(TAG, "Unable to close file reader.", e);
}
}
}
private CommandLine() {}
private static class JavaCommandLine extends CommandLine {
private HashMap<String, String> mSwitches = new HashMap<String, String>();
private ArrayList<String> mArgs = new ArrayList<String>();
private int mArgsBegin = 1;
JavaCommandLine(String[] args) {
if (args == null || args.length == 0 || args[0] == null) {
mArgs.add("");
} else {
mArgs.add(args[0]);
appendSwitchesInternal(args, 1);
}
assert mArgs.size() > 0;
}
private String[] getCommandLineArguments() {
return mArgs.toArray(new String[mArgs.size()]);
}
@Override
public boolean hasSwitch(String switchString) {
return mSwitches.containsKey(switchString);
}
@Override
public String getSwitchValue(String switchString) {
String value = mSwitches.get(switchString);
return value == null || value.isEmpty() ? null : value;
}
@Override
public void appendSwitch(String switchString) {
appendSwitchWithValue(switchString, null);
}
@Override
public void appendSwitchWithValue(String switchString, String value) {
mSwitches.put(switchString, value == null ? "" : value);
String combinedSwitchString = SWITCH_PREFIX + switchString;
if (value != null && !value.isEmpty())
combinedSwitchString += SWITCH_VALUE_SEPARATOR + value;
mArgs.add(mArgsBegin++, combinedSwitchString);
}
@Override
public void appendSwitchesAndArguments(String[] array) {
appendSwitchesInternal(array, 0);
}
private void appendSwitchesInternal(String[] array, int skipCount) {
boolean parseSwitches = true;
for (String arg : array) {
if (skipCount > 0) {
--skipCount;
continue;
}
if (arg.equals(SWITCH_TERMINATOR)) {
parseSwitches = false;
}
if (parseSwitches && arg.startsWith(SWITCH_PREFIX)) {
String[] parts = arg.split(SWITCH_VALUE_SEPARATOR, 2);
String value = parts.length > 1 ? parts[1] : null;
appendSwitchWithValue(parts[0].substring(SWITCH_PREFIX.length()), value);
} else {
mArgs.add(arg);
}
}
}
}
private static class NativeCommandLine extends CommandLine {
@Override
public boolean hasSwitch(String switchString) {
return nativeHasSwitch(switchString);
}
@Override
public String getSwitchValue(String switchString) {
return nativeGetSwitchValue(switchString);
}
@Override
public void appendSwitch(String switchString) {
nativeAppendSwitch(switchString);
}
@Override
public void appendSwitchWithValue(String switchString, String value) {
nativeAppendSwitchWithValue(switchString, value);
}
@Override
public void appendSwitchesAndArguments(String[] array) {
nativeAppendSwitchesAndArguments(array);
}
@Override
public boolean isNativeImplementation() {
return true;
}
}
private static native void nativeReset();
private static native boolean nativeHasSwitch(String switchString);
private static native String nativeGetSwitchValue(String switchString);
private static native void nativeAppendSwitch(String switchString);
private static native void nativeAppendSwitchWithValue(String switchString, String value);
private static native void nativeAppendSwitchesAndArguments(String[] array);
}