List of usage examples for java.io StringReader read
public int read() throws IOException
From source file:com.cloudmine.api.rest.JsonUtilities.java
/** * This method only works if all the values are objects, otherwise it breaks * @param json/* w w w .j av a 2 s .co m*/ * @return */ public static Map<String, String> jsonMapToKeyMap(String json) { //TODO this method is big and kinda gross //TODO Also its kind of broken on input that has top level keys to none json objects if (Strings.isEmpty(json)) { return new HashMap<String, String>(); } try { StringReader reader = new StringReader(json); int readInt; int open = 0; boolean inString = false; boolean escapeNext = false; Map<String, String> jsonMap = new LinkedHashMap<String, String>(); StringBuilder keyBuilder = new StringBuilder(); StringBuilder contentsBuilder = new StringBuilder(); while ((readInt = reader.read()) != -1) { char read = (char) readInt; switch (read) { case '{': if (!inString) open++; break; case '}': if (!inString) { open--; if (open == 1) { //we closed a full block //finish off the recording contentsBuilder.append(read); //get the key String key = keyBuilder.toString(); String[] splitKey = key.split("\""); if (splitKey.length < 1) { throw new ConversionException("Missing key at: " + key); } String parsedKey = splitKey[1]; //get the contents String contents = contentsBuilder.toString(); jsonMap.put(parsedKey, contents); //reset keyBuilder = new StringBuilder(); contentsBuilder = new StringBuilder(); continue; } } break; case '\"': if (escapeNext) { escapeNext = false; } else { inString = !inString; } break; case '\\': escapeNext = true; break; default: escapeNext = false; //only escape the next character } if (open == 1) { keyBuilder.append(read); } else if (open > 1) { contentsBuilder.append(read); } } return jsonMap; } catch (IOException e) { LOG.error("Exception thrown", e); throw new ConversionException("Trouble reading JSON: " + e); } }
From source file:com.silverpeas.tags.servlets.WebFileServer.java
private void displayError(HttpServletResponse res, String userId, String componentId) throws IOException { SilverTrace.info("peasUtil", "WebFileServer.displayError()", "root.MSG_GEN_ENTER_METHOD"); res.setContentType("text/html"); OutputStream out2 = res.getOutputStream(); int read;/*ww w. j a va 2 s . co m*/ StringBuilder message = new StringBuilder(255); message.append("<HTML>"); message.append("<BODY>"); message.append("<h1>Erreur ... </h1>"); message.append( "Le service a rencontré un problème inattendu pendant le traitement de votre demande <br/>"); message.append("</BODY>"); message.append("</HTML>"); StringReader reader = new StringReader(message.toString()); try { read = reader.read(); while (read != -1) { out2.write(read); // writes bytes into the response read = reader.read(); } } catch (Exception e) { SilverTrace.warn("peasUtil", "WebFileServer.displayError", "root.EX_CANT_READ_FILE"); } finally { // we must close the in and out streams try { out2.close(); } catch (Exception e) { SilverTrace.warn("peasUtil", "WebFileServer.displayError", "root.EX_CANT_READ_FILE", "close failed"); } } }
From source file:org.xwt.util.StringFormat.java
public String format(String rawValue) { StringBuffer formattedValue = new StringBuffer(); StringReader rvReader = new StringReader(rawValue); int read = -1; try {//from www. ja va2 s .c o m while ((read = schemaReader.read()) != -1) { char c = (char) read; logger.debug("" + c); if (c == '#') { char tmp = (char) rvReader.read(); logger.debug("" + tmp); formattedValue.append(tmp); } else { formattedValue.append(c); } } this.schemaReader.reset(); } catch (IOException e) { throw new StringFormatException(e); } if (formattedValue.length() != this.schema.length()) { throw new StringFormatException("Failed to format rawValue=" + rawValue + " to schema=" + this.schema + ":output=" + formattedValue.toString()); } return formattedValue.toString(); }
From source file:pt.webdetails.cdc.hazelcast.CfgTestApp.java
private String formatConfig(String config) { StringReader reader = new StringReader(config); StringBuilder sbuild = new StringBuilder(); int ch;/* w ww . jav a2 s . co m*/ int indent = 0; final int INDENT_FACT = 4; try { while ((ch = reader.read()) != -1) { switch (ch) { case '{': case '[': indent++; sbuild.append((char) ch); sbuild.append('\n'); sbuild.append(StringUtils.leftPad("", indent * INDENT_FACT)); break; case '}': case ']': indent--; sbuild.append('\n'); sbuild.append(StringUtils.leftPad("", indent * INDENT_FACT)); sbuild.append((char) ch); // sbuild.append('\n'); // sbuild.append(StringUtils.leftPad("", indent * INDENT_FACT)); break; case ',': sbuild.append((char) ch); sbuild.append('\n'); sbuild.append(StringUtils.leftPad("", indent * INDENT_FACT)); ch = reader.read(); if (ch != ' ') { sbuild.append((char) ch); } break; default: sbuild.append((char) ch); } } } catch (IOException e) { println(e); } return sbuild.append('\n').toString(); }
From source file:com.concursive.connect.web.modules.documents.beans.FileDownload.java
/** * Description of the Method/*ww w .j a va 2 s . c o m*/ * * @param context Description of the Parameter * @param text Description of the Parameter * @throws Exception Description of the Exception */ public void sendTextAsFile(ActionContext context, String text) throws Exception { context.getResponse().setContentType("application/octet-stream"); context.getResponse().setHeader("Content-Disposition", "attachment;filename=" + displayName + ";"); context.getResponse().setContentLength((int) text.length()); ServletOutputStream outputStream = context.getResponse().getOutputStream(); StringReader strReader = new StringReader(text); int data; while ((data = strReader.read()) != -1) { outputStream.write(data); } strReader.close(); outputStream.close(); }
From source file:org.apache.cocoon.forms.transformation.FormsPipelineConfig.java
/** * Replaces JXPath expressions embedded inside #{ and } by their value. * This will parse the passed String looking for #{} occurences and then * uses the {@link #evaluateExpression(String)} to evaluate the found expression. * * @return the original String with it's #{}-parts replaced by the evaulated results. *//*from w w w . j av a 2 s . c o m*/ public String translateText(String original) { if (original == null) { return null; } StringBuffer expression; StringBuffer translated = new StringBuffer(); StringReader in = new StringReader(original); int chr; try { while ((chr = in.read()) != -1) { char c = (char) chr; if (c == '#') { chr = in.read(); if (chr != -1) { c = (char) chr; if (c == '{') { expression = new StringBuffer(); boolean more = true; while (more) { more = false; if ((chr = in.read()) != -1) { c = (char) chr; if (c != '}') { expression.append(c); more = true; } else { translated.append(evaluateExpression(expression.toString()).toString()); } } else { translated.append('#').append('{').append(expression); } } } } else { translated.append((char) chr); } } else { translated.append(c); } } } catch (IOException ignored) { ignored.printStackTrace(); } return translated.toString(); }
From source file:com.stratelia.webactiv.servlets.RestOnlineFileServer.java
private void displayWarningHtmlCode(HttpServletResponse res) throws IOException { StringReader sr = null; OutputStream out2 = res.getOutputStream(); int read;//from ww w . j a va2 s.c om ResourceLocator resourceLocator = new ResourceLocator( "com.stratelia.webactiv.util.peasUtil.multiLang.fileServerBundle", ""); sr = new StringReader(resourceLocator.getString("warning")); try { read = sr.read(); while (read != -1) { SilverTrace.info("peasUtil", "RestOnlineFileServer.displayHtmlCode()", "root.MSG_GEN_ENTER_METHOD", " StringReader read " + read); out2.write(read); // writes bytes into the response read = sr.read(); } } catch (Exception e) { SilverTrace.warn("peasUtil", "RestOnlineFileServer.displayWarningHtmlCode", "root.EX_CANT_READ_FILE", "warning properties"); } finally { try { if (sr != null) { sr.close(); } out2.close(); } catch (Exception e) { SilverTrace.warn("peasUtil", "RestOnlineFileServer.displayHtmlCode", "root.EX_CANT_READ_FILE", "close failed"); } } }
From source file:com.android.mail.browse.MessageHeaderView.java
/** * Returns a short plaintext snippet generated from the given HTML message * body. Collapses whitespace, ignores '<' and '>' characters and * everything in between, and truncates the snippet to no more than 100 * characters.//from ww w .ja v a2 s. c o m * * @return Short plaintext snippet */ @VisibleForTesting static String makeSnippet(final String messageBody) { if (TextUtils.isEmpty(messageBody)) { return null; } final StringBuilder snippet = new StringBuilder(MAX_SNIPPET_LENGTH); final StringReader reader = new StringReader(messageBody); try { int c; while ((c = reader.read()) != -1 && snippet.length() < MAX_SNIPPET_LENGTH) { // Collapse whitespace. if (Character.isWhitespace(c)) { snippet.append(' '); do { c = reader.read(); } while (Character.isWhitespace(c)); if (c == -1) { break; } } if (c == '<') { // Ignore everything up to and including the next '>' // character. while ((c = reader.read()) != -1) { if (c == '>') { break; } } // If we reached the end of the message body, exit. if (c == -1) { break; } } else if (c == '&') { // Read HTML entity. StringBuilder sb = new StringBuilder(); while ((c = reader.read()) != -1) { if (c == ';') { break; } sb.append((char) c); } String entity = sb.toString(); if ("nbsp".equals(entity)) { snippet.append(' '); } else if ("lt".equals(entity)) { snippet.append('<'); } else if ("gt".equals(entity)) { snippet.append('>'); } else if ("amp".equals(entity)) { snippet.append('&'); } else if ("quot".equals(entity)) { snippet.append('"'); } else if ("apos".equals(entity) || "#39".equals(entity)) { snippet.append('\''); } else { // Unknown entity; just append the literal string. snippet.append('&').append(entity); if (c == ';') { snippet.append(';'); } } // If we reached the end of the message body, exit. if (c == -1) { break; } } else { // The current character is a non-whitespace character that // isn't inside some // HTML tag and is not part of an HTML entity. snippet.append((char) c); } } } catch (IOException e) { LogUtils.wtf(LOG_TAG, e, "Really? IOException while reading a freaking string?!? "); } return snippet.toString(); }
From source file:org.eclipse.swt.examples.javaviewer.JavaLineStyler.java
public void parseBlockComments(String text) { blockComments = new ArrayList<>(); StringReader buffer = new StringReader(text); int ch;//from ww w . j ava2 s .co m boolean blkComment = false; int cnt = 0; int[] offsets = new int[2]; boolean done = false; try { while (!done) { switch (ch = buffer.read()) { case -1: { if (blkComment) { offsets[1] = cnt; blockComments.add(offsets); } done = true; break; } case '/': { ch = buffer.read(); if ((ch == '*') && (!blkComment)) { offsets = new int[2]; offsets[0] = cnt; blkComment = true; cnt++; } else { cnt++; } cnt++; break; } case '*': { if (blkComment) { ch = buffer.read(); cnt++; if (ch == '/') { blkComment = false; offsets[1] = cnt; blockComments.add(offsets); } } cnt++; break; } default: { cnt++; break; } } } } catch (IOException e) { // ignore errors } }
From source file:JavaViewer.java
public void parseBlockComments(String text) { blockComments = new Vector(); StringReader buffer = new StringReader(text); int ch;//from ww w. ja va 2s .c om boolean blkComment = false; int cnt = 0; int[] offsets = new int[2]; boolean done = false; try { while (!done) { switch (ch = buffer.read()) { case -1: { if (blkComment) { offsets[1] = cnt; blockComments.addElement(offsets); } done = true; break; } case '/': { ch = buffer.read(); if ((ch == '*') && (!blkComment)) { offsets = new int[2]; offsets[0] = cnt; blkComment = true; cnt++; } else { cnt++; } cnt++; break; } case '*': { if (blkComment) { ch = buffer.read(); cnt++; if (ch == '/') { blkComment = false; offsets[1] = cnt; blockComments.addElement(offsets); } } cnt++; break; } default: { cnt++; break; } } } } catch (IOException e) { // ignore errors } }