/** * Same as s.split(regex) but caches the compiled pattern for speed. * This saves about 10 microseconds (Bulldozer) on subsequent invocations. * * Note: For an input "" this returns [""], not a zero-length array. * This is the same behavior as String.split(). * * @param s non-null * @param regex non-null, don't forget to enclose multiple choices with [] * @throws java.util.regex.PatternSyntaxException unchecked * @since 0.9.24 */ public static String[] split(String s, String regex) { return split(s, regex, 0); }
/** * Overridden to peek at response code. Always returns line. */ @Override protected String filterResponseLine(String line) { String[] s = DataHelper.split(line, " ", 3); if (s.length > 1 && (s[1].startsWith("3") || s[1].startsWith("5"))) _dataExpected = 0; return line; }
/** * @param dflt default or null * @return non-null, fixed size * @since 0.9.1 */ private List<String> getListConfig(String prop, String dflt) { String val = _config.getProperty(prop); if (val == null) val = dflt; if (val == null) return Collections.emptyList(); return Arrays.asList(DataHelper.split(val, ",")); }
private static boolean migrate(String list, File olddir, File todir) { boolean rv = true; String files[] = DataHelper.split(list, ","); for (int i = 0; i < files.length; i++) { File from = new File(olddir, files[i]); if (!copy(from, todir)) { System.err.println("Error copying " + from.getAbsolutePath()); rv = false; } } return rv; }
/** * Tests if <code>dir</code> has been set up as a I2P working directory.<br/> * Returns <code>false</code> if a directory is empty, or contains nothing that * is usually migrated from the base install. * This allows to pre-install plugins before the first router start. * @return true if already set up */ private static boolean isSetup(File dir) { if (dir.isDirectory()) { String[] files = dir.list(); if (files == null) return false; String migrated[] = DataHelper.split(MIGRATE_BASE, ","); for (String file: files) { for (int i = 0; i < migrated.length; i++) { if (file.equals(migrated[i])) return true; } } } return false; }
/** @since 0.9 */ private void initTrackerMap() { String trackers = _config.getProperty(PROP_TRACKERS); if ( (trackers == null) || (trackers.trim().length() <= 0) ) trackers = _context.getProperty(PROP_TRACKERS); if ( (trackers == null) || (trackers.trim().length() <= 0) ) { setDefaultTrackerMap(true); } else { String[] toks = DataHelper.split(trackers, ","); for (int i = 0; i < toks.length; i += 2) { String name = toks[i].trim().replace(",", ","); String url = toks[i+1].trim().replace(",", ","); if ( (name.length() > 0) && (url.length() > 0) ) { String urls[] = DataHelper.split(url, "=", 2); String url2 = urls.length > 1 ? urls[1] : ""; _trackerMap.put(name, new Tracker(name, urls[0], url2)); } } } }
/** * 0x06 len encodedbytes... * @return ASN.1 encoded object * @throws IllegalArgumentException */ private static byte[] getEncodedOID(String oid) { ByteArrayOutputStream baos = new ByteArrayOutputStream(16); baos.write(0x06); // len to be filled in later baos.write(0); String[] f = DataHelper.split(oid, "[.]"); if (f.length < 2) throw new IllegalArgumentException("length: " + f.length); baos.write((40 * Integer.parseInt(f[0])) + Integer.parseInt(f[1])); for (int i = 2; i < f.length; i++) { int v = Integer.parseInt(f[i]); if (v >= 128 * 128 * 128 || v < 0) throw new IllegalArgumentException(); if (v >= 128 * 128) baos.write((v >> 14) | 0x80); if (v >= 128) baos.write((v >> 7) | 0x80); baos.write(v & 0x7f); } byte[] rv = baos.toByteArray(); if (rv.length > 129) throw new IllegalArgumentException(); rv[1] = (byte) (rv.length - 2); return rv; }
/** * @param line part after the #! * @throws IllegalArgumentException on dup key and other errors */ private static OrderedProperties parseProps(String line) throws IllegalArgumentException { line = line.trim(); OrderedProperties rv = new OrderedProperties(); String[] entries = DataHelper.split(line, "#"); for (int i = 0; i < entries.length; i++) { String kv = entries[i]; int eq = kv.indexOf('='); if (eq <= 0 || eq == kv.length() - 1) throw new IllegalArgumentException("No value: \"" + kv + '"'); String k = kv.substring(0, eq); String v = kv.substring(eq + 1); Object old = rv.setProperty(k, v); if (old != null) throw new IllegalArgumentException("Dup key: " + k); } return rv; }
/** @since 0.9.1 */ private void setDefaultTrackerMap(boolean save) { _trackerMap.clear(); for (int i = 0; i < DEFAULT_TRACKERS.length; i += 2) { String name = DEFAULT_TRACKERS[i]; if (name.equals("TheBland") && !SigType.ECDSA_SHA256_P256.isAvailable()) continue; String urls[] = DataHelper.split(DEFAULT_TRACKERS[i+1], "=", 2); String url2 = urls.length > 1 ? urls[1] : null; _trackerMap.put(name, new Tracker(name, urls[0], url2)); } if (save && _config.remove(PROP_TRACKERS) != null) { saveConfig(); } }
/** * Return /proc/cpuinfo as a key-value mapping. * All keys mapped to lower case. * All keys and values trimmed. * For dup keys, first one wins. * Currently used for ARM only. * @return non-null, empty on failure * @since 0.9.1 */ private static Map<String, String> getCPUInfo() { Map<String, String> rv = new HashMap<String, String>(32); BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/cpuinfo"), "ISO-8859-1"), 4096); String line = null; while ( (line = in.readLine()) != null) { String[] parts = DataHelper.split(line, ":", 2); if (parts.length < 2) continue; String key = parts[0].trim().toLowerCase(Locale.US); if (!rv.containsKey(key)) rv.put(key, parts[1].trim()); } } catch (IOException ioe) { warn("Unable to read /proc/cpuinfo", ioe); } finally { if (in != null) try { in.close(); } catch (IOException ioe) {} } return rv; }
/** * @return null if can't be parsed */ public static Comment fromPersistentString(String s) { String[] ss = DataHelper.split(s, ",", 6); if (ss.length != 6) return null; try { long t = Long.parseLong(ss[0]); int r = Integer.parseInt(ss[1]); boolean m = !ss[2].equals("0"); boolean h = !ss[3].equals("0"); Comment rv = new Comment(ss[5], ss[4], r, t, m); if (h) rv.setHidden(); return rv; } catch (NumberFormatException nfe) { return null; } }
while ( (line = br.readLine()) != null) { try { String[] s = DataHelper.split(line.trim(), " ", 2); if (s.length < 2) continue;
if (inputLine.length() == 0) continue; String[] splitLine = DataHelper.split(inputLine, "=", 2); if (splitLine.length == 2) { result.put(splitLine[0].trim().toLowerCase(Locale.US), splitLine[1].trim());
!path.startsWith("/")) return null; String[] parts = DataHelper.split(path.substring(1), "[/\\?&;]", 2); return ConvertToHash.getHash(parts[0]);
if (line.charAt(0) == '#') continue; String[] s = DataHelper.split(line, ","); String lc = s[0].toLowerCase(Locale.US); tlds.add(lc);
/** * Unlike selectProxy(), we parse the option on the fly so it * can be changed. selectProxy() requires restart... * @return null if none * @since 0.9.11 */ private String selectSSLProxy() { String s = getTunnel().getClientOptions().getProperty(PROP_SSL_OUTPROXIES); if (s == null) return null; String[] p = DataHelper.split(s, "[,; \r\n\t]"); if (p.length == 0) return null; // todo doesn't check for "" if (p.length == 1) return p[0]; int i = _context.random().nextInt(p.length); return p[i]; }
/** * Get the saved priorities for a torrent from the config file. * @since 0.8.1 */ public void loadSavedFilePriorities(Snark snark) { MetaInfo metainfo = snark.getMetaInfo(); Storage storage = snark.getStorage(); if (metainfo == null || storage == null) return; if (metainfo.getFiles() == null) return; Properties config = getConfig(snark); String pri = config.getProperty(PROP_META_PRIORITY); if (pri != null) { int filecount = metainfo.getFiles().size(); int[] rv = new int[filecount]; String[] arr = DataHelper.split(pri, ","); for (int i = 0; i < filecount && i < arr.length; i++) { if (arr[i].length() > 0) { try { rv[i] = Integer.parseInt(arr[i]); } catch (Throwable t) {} } } storage.setFilePriorities(rv); } boolean inOrder = Boolean.parseBoolean(config.getProperty(PROP_META_INORDER)); storage.setInOrder(inOrder); }
if (_log.shouldLog(Log.DEBUG)) _log.debug("Status line: [" + line + "]"); String[] toks = DataHelper.split(line, " ", 3); if (toks.length < 2) { if (_log.shouldLog(Log.WARN))
String[] spks = DataHelper.split(spkl, ","); for (String spk : spks) { int colon = spk.indexOf(':');
/** * Create from persistent storage string. * Format: NID:Hash:Destination:port * First 3 in base 64; Destination may be empty string * @throws IllegalArgumentException */ public NodeInfo(String s) throws DataFormatException { super(); String[] parts = DataHelper.split(s, ":", 4); if (parts.length != 4) throw new DataFormatException("Bad format"); byte[] nid = Base64.decode(parts[0]); if (nid == null || nid.length != NID.HASH_LENGTH) throw new DataFormatException("Bad NID"); nID = new NID(nid); byte[] h = Base64.decode(parts[1]); if (h == null || h.length != Hash.HASH_LENGTH) throw new DataFormatException("Bad hash"); //hash = new Hash(h); hash = Hash.create(h); if (parts[2].length() > 0) dest = new Destination(parts[2]); try { port = Integer.parseInt(parts[3]); } catch (NumberFormatException nfe) { throw new DataFormatException("Bad port", nfe); } initialize(); }