Linux内核可以进一步分为三个级别。顶部是系统调用接口,该接口实现诸如读取和写入之类的基本功能。在系统调用接口下方的是内核代码,是与体系结构无关的内核代码。该代码是Linux支持的所有处理器体系结构的通用代码。在此之下是与体系结构相关的代码,该代码通常称为BSP(Board Support Package, 板级支持包)。该代码用作特定体系结构的处理器和特定平台的代码。
// 摘抄自 Scanner JDK15 源码
public final class Scanner implements Iterator<String>, Closeable {
....
// Internal matcher used for finding delimiters
private Matcher matcher;
// Pattern used to delimit tokens
private Pattern delimPattern;
// Pattern found in last hasNext operation
private Pattern hasNextPattern;
private Pattern integerPattern;
private String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
private String non0Digit = "[\\p{javaDigit}&&[^0]]";
private int SIMPLE_GROUP_INDEX = 5;
private String buildIntegerPatternString() {
String radixDigits = digits.substring(0, radix);
// \\p{javaDigit} is not guaranteed to be appropriate
// here but what can we do? The final authority will be
// whatever parse method is invoked, so ultimately the
// Scanner will do the right thing
String digit = "((?i)["+radixDigits+"\\p{javaDigit}])";
String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
groupSeparator+digit+digit+digit+")+)";
// digit++ is the possessive form which is necessary for reducing
// backtracking that would otherwise cause unacceptable performance
String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
String javaStyleInteger = "([-+]?(" + numeral + "))";
String negativeInteger = negativePrefix + numeral + negativeSuffix;
String positiveInteger = positivePrefix + numeral + positiveSuffix;
return "("+ javaStyleInteger + ")|(" +
positiveInteger + ")|(" +
negativeInteger + ")";
}
private Pattern integerPattern() {
if (integerPattern == null) {
integerPattern = patternCache.forName(buildIntegerPatternString());
}
return integerPattern;
}
public int nextInt(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Integer)
&& this.radix == radix) {
int val = ((Integer)typeCache).intValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next int
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Integer.parseInt(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
....
}
Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is “ \t\n\r\f”: the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens. Params: str – a string to be parsed. Throws: NullPointerException – if str is null
public StringTokenizer(String str) {
this(str, " \t\n\r\f", false);
}
输入要解析的字符串和分隔符列表。解析返回结果不包含分隔符
Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens. Note that if delim is null, this constructor does not throw an exception. However, trying to invoke other methods on the resulting StringTokenizer may result in a NullPointerException. Params: str – a string to be parsed. delim – the delimiters. Throws: NullPointerException – if str is null
public StringTokenizer(String str, String delim) {
this(str, delim, false);
}
import java.util.StringTokenizer;
public class ExtendedStringTokenizer extends StringTokenizer {
public ExtendedStringTokenizer(String str) {
super(str);
}
public long nextLong() {
return Long.parseLong(this.nextToken());
}
public double nextDouble() {
return Double.parseDouble(this.nextToken());
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
}
// 注:BufferedReader 可以提供第二个参数,类型为 int,表示缓冲区容量,提供一个较大的容量可以更快
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
ExtendedStringTokenizer initTokenizer = new ExtendedStringTokenizer(reader.readLine());
int lineNum = initTokenizer.nextInt();
for (int i = 0; i < lineNum; i++) {
// TODO: your code
ExtendedStringTokenizer tokenizer = new ExtendedStringTokenizer(reader.readLine());
}
如果读取目标是文件,可以用 new FileReader("filename") 代替 new InputStreamReader(inputStream)
/*********************************************************************/
/** Application Build Config Settings **/
/*********************************************************************/
buildConfig {
appName = project.name // sets value of NAME field
version = project.version // sets value of VERSION field,
// 'unspecified' if project.version is not set
clsName = 'BuildConfig' // sets the name of the BuildConfig class
packageName = fullPackageName // sets the package of the BuildConfig class,
// 'de.fuerstenau.buildconfig' if project.group is not set
charset = 'UTF-8' // sets charset of the generated class,
// 'UTF-8' if not set otherwise
buildConfigField 'String', 'APPLICATION_NAME', applicationName
buildConfigField 'String', 'VERSION_NAME', version as String
buildConfigField 'int', 'VERSION_CODE', versionCode as String
buildConfigField 'long', 'BUILD_UNIXTIME', System.currentTimeMillis() + 'L'
buildConfigField 'java.util.Date', 'BUILD_DATE', 'new java.util.Date(' + System.currentTimeMillis() + 'L)'
buildConfigField 'String', 'BUILD_USER', System.getProperty("user.name")
buildConfigField 'String', 'BUILD_JDK', System.getProperty("java.version")
buildConfigField 'String', 'BUILD_OS', System.getProperty("os.name")
buildConfigField 'boolean','IS_RELEASE_BUILD', isReleaseBuild as String
其后是字段名称'APPLICATION_NAME'(同样用字符串),其后是内容,也必须是字符串。如果内容为 int 或 boolean 等类型,则必须强制转换。
内容部分可以书写代码,以字符串形式书写即可。
4. 让 IDE 识别代码
默认 IDE 不会识别 BuildConfig 生成的代码,为此要手动将其加入 sourceSets
// Add generated build-config directories to the main source set, so that the
// IDE doesn't complain when the app references BuildConfig classes
sourceSets.main.java {
srcDirs += new File(mainSrcDir) //项目本身源代码
srcDirs += new File(buildDir, 'gen/buildconfig/src') //BuildConfig
}
println("${BuildConfig.APPLICATION_NAME} Ver.${BuildConfig.VERSION_NAME} By Kenvix")
println("Built at ${BuildConfig.BUILD_DATE.format()} By ${BuildConfig.BUILD_USER} @ ${BuildConfig.BUILD_OS} JDK ${BuildConfig.BUILD_JDK}")
if (!BuildConfig.IS_RELEASE_BUILD)
println("Debug build")