List of usage examples for org.apache.commons.lang3 CharUtils LF
char LF
To view the source code for org.apache.commons.lang3 CharUtils LF.
Click Source Link
From source file:com.joyent.manta.client.crypto.EncryptedMetadataUtils.java
/** * Converts the headers to be encrypted to a plaintext string. This returns * the value of that will be encrypted and stored as ciphertext. * * @param metadata metadata object containing items to be encrypted * @return string containing headers in the format of <code>Header: Value</code> *///from w ww . j av a 2s . c om public static String encryptedMetadataAsString(final MantaMetadata metadata) { Set<Map.Entry<String, String>> entrySet = metadata.entrySet(); Iterator<Map.Entry<String, String>> iterator = entrySet.iterator(); StringBuilder builder = new StringBuilder(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); if (entry.getKey().startsWith(MantaMetadata.ENCRYPTED_METADATA_PREFIX)) { String line = String.format("%s: %s", entry.getKey(), entry.getValue()); builder.append(line); if (iterator.hasNext()) { builder.append(CharUtils.LF); } } } return builder.toString(); }
From source file:ch.cyberduck.core.AbstractHostCollection.java
/** * @param h Bookmark/*from w w w .j a v a 2 s.c o m*/ * @return User comment for bookmark or null */ public String getComment(final Host h) { if (StringUtils.isNotBlank(h.getComment())) { return StringUtils.remove(StringUtils.remove(h.getComment(), CharUtils.LF), CharUtils.CR); } return null; }
From source file:com.joyent.manta.client.crypto.EncryptedMetadataUtils.java
/** * Parses a plaintext metadata string and converts it into a {@link Map} of * keys and values./* w w w.j av a2s. c o m*/ * * @param plaintext Plaintext binary data in US-ASCII encoding * @return headers as map */ public static Map<String, String> plaintextMetadataAsMap(final byte[] plaintext) { Map<String, String> map = new CaseInsensitiveMap<>(); boolean parsingKey = true; boolean parsingVal = false; final int initialSize = 24; StringBuilder key = new StringBuilder(initialSize); StringBuilder val = new StringBuilder(initialSize); for (int i = 0; i <= plaintext.length; i++) { // Done parsing a line, now we add it to the map if (i == plaintext.length || (char) plaintext[i] == CharUtils.LF) { if (key.length() > 0) { map.put(key.toString(), val.toString()); } key.setLength(0); val.setLength(0); parsingKey = true; parsingVal = false; continue; } char c = (char) plaintext[i]; if (!CharUtils.isAsciiPrintable(c)) { String msg = "Encrypted metadata contained a " + "non-ascii or unprintable character"; throw new MantaClientEncryptionException(msg); } if (c == ':' && parsingKey) { parsingKey = false; continue; } if (c == ' ' && !parsingKey && !parsingVal) { continue; } else if (!parsingKey && !parsingVal) { parsingVal = true; } if (parsingKey) { key.append(c); } if (parsingVal) { val.append(c); } } return map; }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Removes one newline from end of a String if it's there, * otherwise leave it alone. A newline is "{@code \n}", * "{@code \r}", or "{@code \r\n}".</p> * * <p>NOTE: This method changed in 2.0. * It now more closely matches Perl chomp.</p> * * <pre>//from w ww . j a v a 2 s. c o m * StringUtils.chomp(null) = null * StringUtils.chomp("") = "" * StringUtils.chomp("abc \r") = "abc " * StringUtils.chomp("abc\n") = "abc" * StringUtils.chomp("abc\r\n") = "abc" * StringUtils.chomp("abc\r\n\r\n") = "abc\r\n" * StringUtils.chomp("abc\n\r") = "abc\n" * StringUtils.chomp("abc\n\rabc") = "abc\n\rabc" * StringUtils.chomp("\r") = "" * StringUtils.chomp("\n") = "" * StringUtils.chomp("\r\n") = "" * </pre> * * @param str the String to chomp a newline from, may be null * @return String without newline, {@code null} if null String input */ public static String chomp(String str) { if (isEmpty(str)) { return str; } if (str.length() == 1) { char ch = str.charAt(0); if (ch == CharUtils.CR || ch == CharUtils.LF) { return EMPTY; } return str; } int lastIdx = str.length() - 1; char last = str.charAt(lastIdx); if (last == CharUtils.LF) { if (str.charAt(lastIdx - 1) == CharUtils.CR) { lastIdx--; } } else if (last != CharUtils.CR) { lastIdx++; } return str.substring(0, lastIdx); }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Remove the last character from a String.</p> * * <p>If the String ends in {@code \r\n}, then remove both * of them.</p>/*from ww w . jav a 2 s . co m*/ * * <pre> * StringUtils.chop(null) = null * StringUtils.chop("") = "" * StringUtils.chop("abc \r") = "abc " * StringUtils.chop("abc\n") = "abc" * StringUtils.chop("abc\r\n") = "abc" * StringUtils.chop("abc") = "ab" * StringUtils.chop("abc\nabc") = "abc\nab" * StringUtils.chop("a") = "" * StringUtils.chop("\r") = "" * StringUtils.chop("\n") = "" * StringUtils.chop("\r\n") = "" * </pre> * * @param str the String to chop last character from, may be null * @return String without last character, {@code null} if null String input */ public static String chop(String str) { if (str == null) { return null; } int strLen = str.length(); if (strLen < 2) { return EMPTY; } int lastIdx = strLen - 1; String ret = str.substring(0, lastIdx); char last = str.charAt(lastIdx); if (last == CharUtils.LF && ret.charAt(lastIdx - 1) == CharUtils.CR) { return ret.substring(0, lastIdx - 1); } return ret; }
From source file:bfile.util.StringUtils.java
/** * <p>Removes one newline from end of a String if it's there, * otherwise leave it alone. A newline is "{@code \n}", * "{@code \r}", or "{@code \r\n}".</p> * * <p>NOTE: This method changed in 2.0. * It now more closely matches Perl chomp.</p> * * <pre>/*from w w w. ja v a2s . c o m*/ * StringUtils.chomp(null) = null * StringUtils.chomp("") = "" * StringUtils.chomp("abc \r") = "abc " * StringUtils.chomp("abc\n") = "abc" * StringUtils.chomp("abc\r\n") = "abc" * StringUtils.chomp("abc\r\n\r\n") = "abc\r\n" * StringUtils.chomp("abc\n\r") = "abc\n" * StringUtils.chomp("abc\n\rabc") = "abc\n\rabc" * StringUtils.chomp("\r") = "" * StringUtils.chomp("\n") = "" * StringUtils.chomp("\r\n") = "" * </pre> * * @param str the String to chomp a newline from, may be null * @return String without newline, {@code null} if null String input */ public static String chomp(final String str) { if (isEmpty(str)) { return str; } if (str.length() == 1) { final char ch = str.charAt(0); if (ch == CharUtils.CR || ch == CharUtils.LF) { return EMPTY; } return str; } int lastIdx = str.length() - 1; final char last = str.charAt(lastIdx); if (last == CharUtils.LF) { if (str.charAt(lastIdx - 1) == CharUtils.CR) { lastIdx--; } } else if (last != CharUtils.CR) { lastIdx++; } return str.substring(0, lastIdx); }
From source file:bfile.util.StringUtils.java
/** * <p>Remove the last character from a String.</p> * * <p>If the String ends in {@code \r\n}, then remove both * of them.</p>// w ww. ja v a 2 s .c o m * * <pre> * StringUtils.chop(null) = null * StringUtils.chop("") = "" * StringUtils.chop("abc \r") = "abc " * StringUtils.chop("abc\n") = "abc" * StringUtils.chop("abc\r\n") = "abc" * StringUtils.chop("abc") = "ab" * StringUtils.chop("abc\nabc") = "abc\nab" * StringUtils.chop("a") = "" * StringUtils.chop("\r") = "" * StringUtils.chop("\n") = "" * StringUtils.chop("\r\n") = "" * </pre> * * @param str the String to chop last character from, may be null * @return String without last character, {@code null} if null String input */ public static String chop(final String str) { if (str == null) { return null; } final int strLen = str.length(); if (strLen < 2) { return EMPTY; } final int lastIdx = strLen - 1; final String ret = str.substring(0, lastIdx); final char last = str.charAt(lastIdx); if (last == CharUtils.LF && ret.charAt(lastIdx - 1) == CharUtils.CR) { return ret.substring(0, lastIdx - 1); } return ret; }
From source file:org.apache.jmeter.report.core.CsvSampleWriter.java
@Override public long write(Sample sample) { Validate.validState(writer != null, "No writer set! Call setWriter() first!"); StringBuilder row = new StringBuilder(); char[] specials = new char[] { separator, CSVSaveService.QUOTING_CHAR, CharUtils.CR, CharUtils.LF }; for (int i = 0; i < columnCount; i++) { String data = sample.getData(i); row.append(CSVSaveService.quoteDelimiters(data, specials)).append(separator); }//from www.j a va 2 s .c o m row.setLength(row.length() - 1); writer.println(row.toString()); sampleCount++; return sampleCount; }
From source file:org.pepstock.jem.jppf.ExecuteManager.java
/** * Create a JPPF job that can be submitted for execution. * // w ww. ja v a 2s . c o m * @param runnable Class name of Runnable or JPPFTask to execute * @param parallelTaskNumber Number of parallel task to submit * @param xmlContext InitialContext, serialized in XML string * @return an instance of the {@link org.jppf.client.JPPFJob JPPFJob} class. * @throws NamingException * @throws IOException * @throws JPPFException * @throws JPFFException * if an error occurs while creating the job or adding tasks. */ private static JPPFJob createJob(String xmlContext) throws JPPFMessageException, NamingException, IOException, JPPFException { XStream xstream = new XStream(); // reads all properties String jobName = JPPFConfiguration.getProperties().getProperty(Keys.JEM_JOB_NAME); String taskName = JPPFConfiguration.getProperties().getProperty(Keys.JEM_TASK_NAME); String runnable = JPPFConfiguration.getProperties().getProperty(Keys.JEM_RUNNABLE); int parallelTaskNumber = JPPFConfiguration.getProperties().getInt(Keys.JEM_TASK_NUMBER); // creates a data provider to pass initial context and number of parallel task MemoryMapDataProvider provider = new MemoryMapDataProvider(); provider.setValue(Keys.JEM_CONTEXT, xmlContext); provider.setValue(Keys.JEM_TASK_NUMBER, String.valueOf(parallelTaskNumber)); // checks if CHUNK is set if (JPPFConfiguration.getProperties().containsKey(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION)) { // gets data description String dataDescription = JPPFConfiguration.getProperties() .getProperty(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION); File file = null; InitialContext ic = ContextUtils.getContext(); // lookup for JNDI reference // needs file name because chunk is based on RandomFileAccess // that wants a FILE and not inputstream // so gets data description implementation object // to get REAL FILE NamingEnumeration<NameClassPair> list = ic.list(dataDescription); while (list.hasMore()) { NameClassPair pair = list.next(); // checks if is datastream // only datastreams are searched if (pair.getName().equalsIgnoreCase(dataDescription) && pair instanceof DataStreamNameClassPair) { DataStreamNameClassPair dsPair = (DataStreamNameClassPair) pair; DataStreamReference prevReference = (DataStreamReference) dsPair.getObject(); // gets data description XML definition StringRefAddr sra = (StringRefAddr) prevReference.get(StringRefAddrKeys.DATASTREAMS_KEY); // creates datadescription implementatio to get file DataDescriptionImpl ddImpl = (DataDescriptionImpl) xstream.fromXML(sra.getContent().toString()); file = ddImpl.getDatasets().iterator().next().getRealFile(); // leaves while break; } } // if file is null // data description is not found if (file == null) { throw new JPPFMessageException(JPPFMessage.JEMJ019E, dataDescription); } // calculated buffersize long bufferSize = file.length() / parallelTaskNumber; // using delimiter, creates chunk list List<ChunkDefinition> chunks = null; String delimiter = JPPFConfiguration.getProperties().getProperty(Keys.JEM_DELIMITER); String delimiterString = JPPFConfiguration.getProperties().getProperty(Keys.JEM_DELIMITER_STRING); if (delimiter != null) { // delimiter default LF char splitter = CharUtils.LF; // if delimiter is defined in BYTE mode // calculate char if (delimiter.toLowerCase().startsWith("0x")) { delimiter = StringUtils.substringAfter(delimiter.toLowerCase(), "0x"); splitter = (char) Integer.parseInt(delimiter, 16); } else { // if uses a escape java char splitter = StringEscapeUtils.unescapeJava(delimiter).charAt(0); } String displayDelimiter = Integer.toHexString((byte) splitter) + "(" + splitter + ")"; LogAppl.getInstance().emit(JPPFMessage.JEMJ020I, displayDelimiter); // calculates chunks chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize, splitter); provider.setValue(Keys.JEM_DELIMITER, splitter); } else if (delimiterString != null) { LogAppl.getInstance().emit(JPPFMessage.JEMJ020I, delimiterString); // calculates chunks chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize, delimiterString); provider.setValue(Keys.JEM_DELIMITER, delimiterString); } else { // if delimiter and delimiterString are missing, uses default delimiter (System.getProperty("line.separator") chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize); } // changes parallel task // because chunk calculation tries to maintain the same // number of task but usually there are less after // chunk list creation parallelTaskNumber = chunks.size(); LogAppl.getInstance().emit(JPPFMessage.JEMJ021I, chunks); // serializes and saves on data provider all necessary data provider.setValue(Keys.JEM_TASK_NUMBER, String.valueOf(parallelTaskNumber)); provider.setValue(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION, dataDescription); provider.setValue(Keys.JEM_CHUNKS, xstream.toXML(chunks)); } LogAppl.getInstance().emit(JPPFMessage.JEMJ023I, parallelTaskNumber); // checks if merged data decritpion is an argument if (JPPFConfiguration.getProperties().containsKey(Keys.JEM_MERGED_DATA_DESCRIPTION)) { // loads a list with all temporary files for tasks // to use to write partial output for (int i = 0; i < parallelTaskNumber; i++) { File temp = File.createTempFile("task[" + i + "]-merge", ".jemjppf"); temp.deleteOnExit(); TEMPORARY_FILES.add(temp); LogAppl.getInstance().emit(JPPFMessage.JEMJ022I, temp.getAbsolutePath(), i); } // sets data provider provider.setValue(Keys.JEM_MERGED_DATA_DESCRIPTION, JPPFConfiguration.getProperties().getProperty(Keys.JEM_MERGED_DATA_DESCRIPTION)); provider.setValue(Keys.JEM_TEMPORARY_FILES, xstream.toXML(TEMPORARY_FILES)); } // create a JPPF job JPPFJob job = new JPPFJob(provider); // give this job a readable unique id that we can use to monitor and // manage it. job.setName(jobName + "." + taskName); // Checks what instance has been past to distribute on grid try { Object clazz = Class.forName(runnable).newInstance(); if (clazz instanceof JPPFTask) { for (int i = 0; i < parallelTaskNumber; i++) { JPPFTask t = new WrapperJPPFTask((JPPFTask) clazz); // add a task to the job. job.addTask(t); } } else if (clazz instanceof Runnable) { for (int i = 0; i < parallelTaskNumber; i++) { JPPFTask t = new RunnableTask((Runnable) clazz); // add a task to the job. job.addTask(t); } } else { throw new JPPFMessageException(JPPFMessage.JEMJ002E, runnable, Runnable.class.getName(), JPPFTask.class.getName()); } } catch (InstantiationException e) { throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(), JPPFTask.class.getName()); } catch (IllegalAccessException e) { throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(), JPPFTask.class.getName()); } catch (ClassNotFoundException e) { throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(), JPPFTask.class.getName()); } // there is no guarantee on the order of execution of the tasks, // however the results are guaranteed to be returned in the same order // as the tasks. return job; }