Example usage for java.io PushbackReader PushbackReader

List of usage examples for java.io PushbackReader PushbackReader

Introduction

In this page you can find the example usage for java.io PushbackReader PushbackReader.

Prototype

public PushbackReader(Reader in) 

Source Link

Document

Creates a new pushback reader with a one-character pushback buffer.

Usage

From source file:gridool.db.partitioning.phihash.csv.grace.CsvGraceHashPartitioningTask.java

private static final CsvReader getCsvReader(final String csvPath, final char filedSeparator,
        final char quoteChar) throws GridException {
    final Reader reader;
    try {//from w w w.j  a va2  s  .c o  m
        FileInputStream fis = new FileInputStream(csvPath);
        FastBufferedInputStream bis = new FastBufferedInputStream(fis, csvInputBufSize);
        reader = new InputStreamReader(bis, "UTF-8");
    } catch (FileNotFoundException fne) {
        LOG.error(fne);
        throw new GridException("CSV file not found: " + csvPath, fne);
    } catch (UnsupportedEncodingException uee) {
        LOG.error(uee);
        throw new IllegalStateException(uee); // should never happens
    }
    PushbackReader pushback = new PushbackReader(reader);
    return new SimpleCsvReader(pushback, filedSeparator, quoteChar);
}

From source file:ca.uhn.fhir.rest.method.MethodUtil.java

public static MethodOutcome process2xxResponse(FhirContext theContext, int theResponseStatusCode,
        String theResponseMimeType, Reader theResponseReader, Map<String, List<String>> theHeaders) {
    List<String> locationHeaders = new ArrayList<String>();
    List<String> lh = theHeaders.get(Constants.HEADER_LOCATION_LC);
    if (lh != null) {
        locationHeaders.addAll(lh);/*w ww .  j a  v a2s . co  m*/
    }
    List<String> clh = theHeaders.get(Constants.HEADER_CONTENT_LOCATION_LC);
    if (clh != null) {
        locationHeaders.addAll(clh);
    }

    MethodOutcome retVal = new MethodOutcome();
    if (locationHeaders != null && locationHeaders.size() > 0) {
        String locationHeader = locationHeaders.get(0);
        BaseOutcomeReturningMethodBinding.parseContentLocation(theContext, retVal, locationHeader);
    }
    if (theResponseStatusCode != Constants.STATUS_HTTP_204_NO_CONTENT) {
        EncodingEnum ct = EncodingEnum.forContentType(theResponseMimeType);
        if (ct != null) {
            PushbackReader reader = new PushbackReader(theResponseReader);

            try {
                int firstByte = reader.read();
                if (firstByte == -1) {
                    BaseOutcomeReturningMethodBinding.ourLog.debug("No content in response, not going to read");
                    reader = null;
                } else {
                    reader.unread(firstByte);
                }
            } catch (IOException e) {
                BaseOutcomeReturningMethodBinding.ourLog.debug("No content in response, not going to read", e);
                reader = null;
            }

            if (reader != null) {
                IParser parser = ct.newParser(theContext);
                IBaseResource outcome = parser.parseResource(reader);
                if (outcome instanceof IBaseOperationOutcome) {
                    retVal.setOperationOutcome((IBaseOperationOutcome) outcome);
                } else {
                    retVal.setResource(outcome);
                }
            }

        } else {
            BaseOutcomeReturningMethodBinding.ourLog.debug("Ignoring response content of type: {}",
                    theResponseMimeType);
        }
    }
    return retVal;
}

From source file:nl.armatiek.xslweb.serializer.RequestSerializer.java

private void serializeBody(List<FileItem> fileItems) throws Exception {
    if (!req.getMethod().equals("POST") || fileItems != null) {
        return;/*from   w ww .  j  a  va 2  s  .  c  o m*/
    }
    PushbackReader pushbackReader = new PushbackReader(req.getReader());
    int b = pushbackReader.read();
    if (b == -1) {
        return;
    }
    pushbackReader.unread(b);
    xsw.writeStartElement(URI, "body");
    String contentType = req.getContentType();
    if (contentType != null && contentType.contains(";")) {
        contentType = contentType.split(";")[0].trim();
    }
    if ((contentType != null) && (contentType.startsWith("text/xml")
            || contentType.startsWith("application/xml") || contentType.endsWith("+xml"))) {
        getFilteredXMLReader().parse(new InputSource(pushbackReader));
    } else if ((contentType != null) && contentType.startsWith("text/plain")) {
        xsw.writeCharacters(IOUtils.toString(pushbackReader));
    } else {
        xsw.writeCData(Base64.encodeBase64String(IOUtils.toByteArray(pushbackReader, "UTF-8")));
    }
    xsw.writeEndElement();
}

From source file:org.eclipse.jubula.client.core.utils.GuiParamValueConverter.java

/** create tokens from gui string */
void createTokens() {
    Parser parser = new Parser(new JubulaParameterLexer(
            new PushbackReader(new StringReader(StringUtils.defaultString(getGuiString())))));
    ParsedParameter parsedParam = new ParsedParameter(true, getCurrentNode(), getDesc());
    try {/*from   w ww .jav a  2 s.  c o  m*/
        parser.parse().apply(parsedParam);
        setTokens(parsedParam.getTokens());
    } catch (LexerException e) {
        createErrors(e, getGuiString());
    } catch (ParserException e) {
        createErrors(e, getGuiString());
    } catch (IOException e) {
        LOG.error(Messages.ParameterParsingErrorOccurred, e);
        createErrors(e, getGuiString());
    } catch (SemanticParsingException e) {
        createErrors(e, getGuiString());
    }
}

From source file:org.eclipse.jubula.client.core.utils.ModelParamValueConverter.java

/**
 * @{inheritDoc}//from  w w w . ja  v a 2  s .com
 */
void createTokens() {
    Parser parser = new Parser(new JubulaParameterLexer(
            new PushbackReader(new StringReader(StringUtils.defaultString(getModelString())))));
    ParsedParameter parsedParam = new ParsedParameter(false, getCurrentNode(), getDesc());
    try {
        parser.parse().apply(parsedParam);
        setTokens(parsedParam.getTokens());
    } catch (LexerException e) {
        createErrors(e, getModelString());
    } catch (ParserException e) {
        createErrors(e, getModelString());
    } catch (IOException e) {
        LOG.error(Messages.ParameterParsingErrorOccurred, e);
        createErrors(e, getModelString());
    } catch (SemanticParsingException e) {
        createErrors(e, getModelString());
    }
}

From source file:org.eclipse.jubula.client.core.utils.SimpleStringConverter.java

/** create tokens from gui string */
void createTokens() {
    Parser parser = new Parser(new JubulaParameterLexer(
            new PushbackReader(new StringReader(StringUtils.defaultString(getGuiString())))));
    ParsedParameter parsedParam = new ParsedParameter(true, null, null);
    try {/*from   www.  j  a  va 2  s  .  c o m*/
        parser.parse().apply(parsedParam);
        List<IParamValueToken> liste = parsedParam.getTokens();
        setTokens(liste);
    } catch (LexerException e) {
        createErrors(e, getGuiString());
    } catch (ParserException e) {
        createErrors(e, getGuiString());
    } catch (IOException e) {
        LOG.error(Messages.ParameterParsingErrorOccurred, e);
        createErrors(e, getGuiString());
    } catch (SemanticParsingException e) {
        createErrors(e, getGuiString());
    }
}

From source file:org.limy.eclipse.qalab.outline.sequence.SequenceImageCreator.java

/**
 * V?[PX?}?? layoutData, pngFile ?o?B// w w w  .j a va2 s. c om
 * @param root V?[PX?}Bean
 * @throws IOException I/OO
 * @throws ParserException 
 */
private void writeSequence(SequenceBean root) throws IOException, ParserException {
    StringWriter out = new StringWriter();
    Context context = new VelocityContext();
    context.put("root", root);
    VelocitySupport.write(new File(LimyQalabPlugin.getDefault().getPluginRoot(), "resource/sequence/index.vm")
            .getAbsolutePath(), context, out);
    File txtFile = LimyQalabUtils.createTempFile(env.getProject(), "sequence.txt");
    pngFile = LimyQalabUtils.createTempFile(env.getProject(), "sequence.png");
    FileUtils.writeByteArrayToFile(txtFile, out.toString().getBytes());

    Parser parser = ParserFactory.getInstance().getDefaultParser();
    NodeFactory nodeFactory = ParserFactory.getInstance().getNodeFactoryForParser(parser);
    Diagram diagram = new Diagram(parser, nodeFactory);

    PushbackReader reader = new PushbackReader(new FileReader(txtFile));
    try {
        diagram.parse(reader);
    } finally {
        reader.close();
    }

    Model model = new Model(new ExceptionHandler() {
        public void exception(Exception e) {
            e.printStackTrace();
        }
    }, diagram);

    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bi.createGraphics();
    layoutData = new LayoutData(new SwingStringMeasure(graphics));
    diagram.layout(layoutData);

    int height = layoutData.getHeight();
    int width = layoutData.getWidth();

    BufferedImage png = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D pngGraphics = png.createGraphics();
    pngGraphics.setClip(0, 0, width, height);
    Map<Key, Object> hintsMap = new HashMap<Key, Object>();
    hintsMap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    pngGraphics.addRenderingHints(hintsMap);
    pngGraphics.setBackground(Prefs.getColorValue(Prefs.BACKGROUND_COLOR));
    pngGraphics.fillRect(0, 0, width, height);

    SwingPainter painter = new SwingPainter();
    painter.setGraphics(pngGraphics);
    model.layout(layoutData);
    layoutData.paint(painter);

    ImageIO.write(png, "png", pngFile);
}