List of usage examples for java.nio CharBuffer subSequence
public abstract CharSequence subSequence(int start, int end);
From source file:Main.java
public static void main(String[] args) { CharBuffer cb1 = CharBuffer.allocate(10); cb1.append("java2s.com"); cb1.rewind();/*from w w w . j a v a2s. com*/ System.out.println(Arrays.toString(cb1.array())); CharBuffer cb2 = cb1.subSequence(0, 2); System.out.println(cb2); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer buf = ByteBuffer.allocate(100); CharBuffer cbuf = buf.asCharBuffer(); cbuf.put("a string"); int start = 2; // start is relative to cbuf's current position int end = 5;/*from www .j ava 2 s . co m*/ CharSequence sub = cbuf.subSequence(start, end); // str }
From source file:gephi.spade.panel.fcsFile.java
/** * readASCIIData ---/*from ww w . j ava 2 s.c o m*/ * <p> * Reads ASCII values in list mode in the DATA segment and updates eventList * with the integer values of the values. * </p> * * @param data * <code>ByteBuffer</code> containing the DATA segment of the * underlying file. */ private void readASCIIData(ByteBuffer data) { /** * Calculate the number of characters in each event of the flow file */ // Initialize the number of characters in each event to 0 int numCharsPerEvent = 0; // Loop through all the parameters adding the number of characters in // each parameter for (int i = 0; i < parameters; i++) { numCharsPerEvent += channelBits[i]; } // Allocate the eventList eventList = new double[parameters][totalEvents]; // Convert the byte buffer into a char buffer - doesn't get any easier CharBuffer cb = data.asCharBuffer(); // Initialize the current character to 0 int currChar = 0; for (int i = 0; i < totalEvents; i++) { for (int j = 0; j < parameters; j++) { try { // Store the value into the array eventList[j][i] = Integer .parseInt(cb.subSequence(currChar, currChar + channelBits[i]).toString()); } catch (NumberFormatException nfe) { eventList[j][i] = 0; } // Increment the current character currChar += channelBits[i]; } } }
From source file:org.nuxeo.ecm.platform.filemanager.service.extension.NoteImporter.java
protected static String guessEncoding(Blob blob) throws IOException { // encoding already known? if (blob.getEncoding() != null) { return null; }//from w w w .j a v a 2 s . c om // bad mime type? String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { // not a text file, we shouldn't be in the Note importer return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { // charset specified in MIME type String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { // charset detected from the actual bytes CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } // now convert the string according to the charset, and fallback on others if not possible for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { // remove BOM cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { // illegal charset } catch (CharacterCodingException e) { // could not decode } } // nothing worked, use platform return null; }
From source file:org.wings.session.SmartURLsFilter.java
private static void encode(ByteArrayOutputStream bytes, ServletOutputStream out) throws IOException { String regex = "(href|src|action) *= *\"([^\"]*)\""; SmartURLsFilter smartURLsFilter = new SmartURLsFilter(); String replacement = smartURLsFilter.parameterSeparator + "$2" + smartURLsFilter.nameValueSeparator + "$4"; smartURLsFilter.encodePattern = Pattern .compile("(" + "\\?|&" + ")([a-zA-Z0-9%+.-[*]_]*)" + "(" + "=" + ")([a-zA-Z0-9%+.-[*]_=/]*)"); CharBuffer chars = CharBuffer.wrap(bytes.toString()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(chars); int pos = 0;/*from w w w. j a va 2 s . c om*/ while (matcher.find()) { out.print(chars.subSequence(pos, matcher.start()).toString()); pos = matcher.end(); Matcher matcher2 = smartURLsFilter.encodePattern.matcher(matcher.group(2)); String group2 = matcher2.replaceAll(replacement); out.print(matcher.group(1) + "=\"" + group2 + "\""); } out.print(chars.subSequence(pos, bytes.size()).toString()); }