Example usage for java.io PushbackReader close

List of usage examples for java.io PushbackReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create a new PushBack reader based on our string reader
    PushbackReader pr = new PushbackReader(sr);

    try {//from ww w. ja v  a  2  s  .c  om
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }
        // unread a character
        pr.unread('F');

        // read the next char, which is the one we unread
        char c = (char) pr.read();

        System.out.println(c);

        pr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create a new PushBack reader based on our string reader
    PushbackReader pr = new PushbackReader(sr, 20);

    try {/*from  w  ww . ja v  a2 s  .  c o m*/
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }
        // unread a character
        pr.unread('F');

        // read the next char, which is the one we unread
        char c = (char) pr.read();

        System.out.println(c);

        pr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

public String format(String toFormat) {
    PushbackReader in = new PushbackReader(new StringReader(toFormat), 64 * 1024);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {/*from   www  . j a v a 2 s .  co m*/
        Start parsed = parser.parse();
        return new TextUMLFormatter().format(parsed.getPStart(), parser.ignoredTokens);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // fall through
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return toFormat;
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

private Start parse(Reader source, IProblemTracker problems) throws CoreException {
    ProblemBuilder<Node> problemBuilder = new ProblemBuilder<Node>(problems, new SCCTextUMLSourceMiner());
    PushbackReader in = new PushbackReader(source, 64 * 1024);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {//from ww w  .j av  a2 s  . c o  m
        return parser.parse();
    } catch (ParserException e) {
        if (problems != null)
            problemBuilder.addProblem(
                    new SyntaxProblem("Found: '" + e.getToken().getText() + "'. " + e.getMessage()),
                    e.getToken());
    } catch (LexerException e) {
        if (problems != null) {
            SyntaxProblem problem = new SyntaxProblem(e.getMessage());
            problem.setAttribute(IProblem.LINE_NUMBER, SCCTextUMLSourceMiner.parseLineNumber(e.getMessage()));
            problemBuilder.addProblem(problem, null);
        }
    } catch (IOException e) {
        IStatus status = new Status(IStatus.ERROR, TextUMLConstants.PLUGIN_ID, 0,
                "Error reading source unit: " + source.toString(), e);
        throw new CoreException(status);
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return null;
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLCompiler.java

public String findModelName(String toParse) {
    PushbackReader in = new PushbackReader(new StringReader(toParse), 1);
    Lexer lexer = new Lexer(in);
    Parser parser = new Parser(lexer);
    try {/*from w w  w  . j  a v  a 2  s.c  om*/
        Start parsed = parser.parse();
        final String[] packageIdentifier = { null };
        parsed.getPStart().apply(new DepthFirstAdapter() {
            @Override
            public void caseAStart(AStart node) {
                ((APackageHeading) node.getPackageHeading()).getQualifiedIdentifier()
                        .apply(new DepthFirstAdapter() {
                            @Override
                            public void caseTIdentifier(TIdentifier node) {
                                if (packageIdentifier[0] == null)
                                    packageIdentifier[0] = Util.stripEscaping(node.getText());
                            }
                        });
            }
        });
        return packageIdentifier[0];
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        // fall through
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // not interested in failures while closing
        }
    }
    return null;
}

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

/**
 * V?[PX?}?? layoutData, pngFile ?o?B//  www .j  av a 2  s.  c  o  m
 * @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);
}