public static void main(String[] args) { System.out.println("** TestConstructor"); XLog.initializeLogger(); new Animal(); new Duck(); new RedDuck(); new WhiteDuck(); new DottedRedDuck(); XLog.closeLogger(); TLog.printDebug(); }
/** * 读取配置文件/system/etc/vold.fstab获取设备 如 [/mnt/sdcard, /mnt/extsdcard, /mnt/fat] */ private static void readVold(List<String> mVold) { mVold.add("/mnt/sdcard"); try { File voldFile = new File("/system/etc/vold.fstab"); if (voldFile.exists()) { Scanner scanner = new Scanner(voldFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("dev_mount")) { String[] lineElements = line.split(" "); String element = lineElements[2]; if (element.contains(":")) { element = element.substring(0, element.indexOf(":")); } if (!element.equals("/mnt/sdcard")) { mVold.add(element); } } } } } catch (IOException e) { mVold.clear(); XLog.e(CLASS_NAME, e.getMessage()); } }
public static String getHtmlString(String url) { try { Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); return response.body().string(); } catch (Exception e) { XLog.d("win", "lllll" + e.toString()); return ""; } }
/** 读取配置文件/proc/mounts 获取文件系统挂载信息 如 [/mnt/sdcard, /mnt/extsdcard] */ private static void readMounts(List<String> mMounts) { mMounts.add("/mnt/sdcard"); try { File mountFile = new File("/proc/mounts"); if (mountFile.exists()) { Scanner scanner = new Scanner(mountFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("/dev/block/vold/")) { String[] lineElements = line.split(" "); String element = lineElements[1]; if (!element.equals("/mnt/sdcard")) { mMounts.add(element); } } } } } catch (IOException e) { mMounts.clear(); XLog.e(CLASS_NAME, e.getMessage()); } }
/** Utility class to write/read Hadoop writables to/from a byte array. */ public class WritableUtils { public static XLog LOG = XLog.getLog(WritableUtils.class); /** * Write a writable to a byte array. * * @param writable writable to write. * @return array containing the serialized writable. */ public static byte[] toByteArray(Writable writable) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream daos = new DataOutputStream(baos); writable.write(daos); daos.close(); return baos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } } /** * Read a writable from a byte array. * * @param array byte array with the serialized writable. * @param clazz writable class. * @return writable deserialized from the byte array. */ public static <T extends Writable> T fromByteArray(byte[] array, Class<T> clazz) { try { T o = (T) ReflectionUtils.newInstance(clazz, null); o.readFields(new DataInputStream(new ByteArrayInputStream(array))); return o; } catch (IOException ex) { throw new RuntimeException(ex); } } private static final String NULL = "||"; /** * Write a string to a data output supporting <code>null</code> values. * * <p>It uses the '||' token to represent <code>null</code>. * * @param dataOutput data output. * @param str string to write. * @throws IOException thrown if the string could not be written. */ public static void writeStr(DataOutput dataOutput, String str) throws IOException { str = (str != null) ? str : NULL; dataOutput.writeUTF(str); } /** * Read a string from a data input supporting <code>null</code> values. * * <p>It uses the '||' token to represent <code>null</code>. * * @param dataInput data input. * @return read string, <code>null</code> if the '||' token was read. * @throws IOException thrown if the string could not be read. */ public static String readStr(DataInput dataInput) throws IOException { String str = dataInput.readUTF(); return (str.equals(NULL)) ? null : str; } /** * Read list. * * @param <T> the generic type * @param dataInput the data input * @param clazz the clazz * @return the list * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> List<T> readList(DataInput dataInput, Class<T> clazz) throws IOException { List<T> a = new ArrayList<T>(); int count = dataInput.readInt(); for (int i = 0; i < count; i++) { T o = (T) ReflectionUtils.newInstance(clazz, null); o.readFields(dataInput); a.add(o); } return a; } public static List<String> readStringList(DataInput dataInput) throws IOException { List<String> a = new ArrayList<String>(); int count = dataInput.readInt(); for (int i = 0; i < count; i++) { a.add(readBytesAsString(dataInput)); } return a; } /** * Write list. * * @param <T> the generic type * @param dataOutput the data output * @param list the list * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> void writeList(DataOutput dataOutput, List<T> list) throws IOException { dataOutput.writeInt(list.size()); for (T t : list) { t.write(dataOutput); } } public static void writeStringList(DataOutput dataOutput, List<String> list) throws IOException { dataOutput.writeInt(list.size()); for (String str : list) { writeStringAsBytes(dataOutput, str); } } /** * Write map. * * @param <T> the generic type * @param dataOutput the data output * @param map the map * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> void writeMap(DataOutput dataOutput, Map<String, T> map) throws IOException { dataOutput.writeInt(map.size()); for (Entry<String, T> t : map.entrySet()) { writeStringAsBytes(dataOutput, t.getKey()); t.getValue().write(dataOutput); } } /** * Write map with list. * * @param <T> the generic type * @param dataOutput the data output * @param map the map * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> void writeMapWithList( DataOutput dataOutput, Map<String, List<T>> map) throws IOException { dataOutput.writeInt(map.size()); for (Entry<String, List<T>> t : map.entrySet()) { writeStringAsBytes(dataOutput, t.getKey()); writeList(dataOutput, t.getValue()); } } /** * Read map. * * @param <T> the generic type * @param dataInput the data input * @param clazz the clazz * @return the map * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> Map<String, T> readMap(DataInput dataInput, Class<T> clazz) throws IOException { Map<String, T> map = new HashMap<String, T>(); int count = dataInput.readInt(); for (int i = 0; i < count; i++) { String key = readBytesAsString(dataInput); T value = (T) ReflectionUtils.newInstance(clazz, null); value.readFields(dataInput); map.put(key, value); } return map; } /** * Read map with list. * * @param <T> the generic type * @param dataInput the data input * @param clazz the clazz * @return the map * @throws IOException Signals that an I/O exception has occurred. */ public static <T extends Writable> Map<String, List<T>> readMapWithList( DataInput dataInput, Class<T> clazz) throws IOException { Map<String, List<T>> map = new HashMap<String, List<T>>(); int count = dataInput.readInt(); for (int i = 0; i < count; i++) { String key = readBytesAsString(dataInput); map.put(key, readList(dataInput, clazz)); } return map; } public static void writeStringAsBytes(DataOutput dOut, String value) throws IOException { byte[] data = value.getBytes(CodecFactory.UTF_8_ENCODING); dOut.writeInt(data.length); dOut.write(data); } public static String readBytesAsString(DataInput dIn) throws IOException { int length = dIn.readInt(); byte[] data = new byte[length]; dIn.readFully(data); return new String(data, CodecFactory.UTF_8_ENCODING); } }
public Duck() { super(); XLog.log("Creating a duck", 99); System.out.println("-- Duck"); }