Java PushbackInputStream Example

  1. /*
  2. Java PushbackInputStream Example
  3. This example shows how to use Java PushbackInputStream class provided in
  4. java.io package.
  5.  
  6. */
  7.  
  8. import java.io.ByteArrayInputStream;
  9. import java.io.IOException;
  10. import java.io.PushbackInputStream;
  11.  
  12. public class JavaPushbackInputStreamExamples {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. //example expression you want to evaluate in your program
  17. String strExpression = "a = a++ + b;";
  18.  
  19. /*
  20. * Here, while, evaluating the expression, You need to know
  21. * whether it is ++ operator or + plus operator. Basically,
  22. * you need to read ahead or peek whether another + follows
  23. * immediately after one + sign.
  24. */
  25.  
  26. //get bytes from the expression string
  27. byte bytes[] = strExpression.getBytes();
  28.  
  29. //create stream from expression bytes
  30. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  31.  
  32. /*
  33. * PushbackInputStream provides below given two constructors.
  34. *
  35. * 1) PushbackInputStream(InputStream is)
  36. * creates PushbackInputStream from provided InputStream object
  37. *
  38. * 2)PushbackInputStream(InputStream is, int size)
  39. * Creates PushbackInputStream from specified InputStream object
  40. * with pushback buffer of specified size.
  41. */
  42. //create PushbackInputStream from ByteArrayInputStream
  43. PushbackInputStream pis = new PushbackInputStream(bis);
  44.  
  45. int ch;
  46.  
  47. try
  48. {
  49.  
  50. while( (ch = pis.read())!= -1)
  51. {
  52. //we encountered first + operator
  53. if(ch == '+')
  54. {
  55. //peek to see if another + follows
  56. if( (ch = pis.read()) == '+')
  57. {
  58. System.out.print("Plus Plus");
  59. }
  60. else
  61. {
  62. //push back one position as we did not get another +
  63. pis.unread(ch);
  64.  
  65. System.out.print("+");
  66. }
  67. }
  68. else
  69. {
  70. //print the character
  71. System.out.print((char)ch);
  72. }
  73. }
  74. }
  75. catch(IOException ioe)
  76. {
  77. System.out.println("Exception while reading" + ioe);
  78. }
  79. }
  80. }
  81.  
  82. /*
  83. Output of this program would be,
  84. a = aPlus Plus + b;
  85. */

Post new comment

To combat spam, please enter the code in the image.

Related Java Examples



    Suggested Reading

    Oracle Magazine
    Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
    Cost: FREE

    View/Subscribe
    NASA Tech Briefs
    Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
    Cost: FREE

    View/Subscribe
    FierceBiotech IT
    Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
    Cost: FREE

    View/Subscribe
    Simply SQL
    Simply SQL is a practical step-by-step guide to writing SQL.
    Cost: FREE

    View/Subscribe
    Simply JavaScript
    Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
    Cost: FREE

    View/Subscribe
    PCMag.com's What's New Now
    Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
    Cost: FREE

    View/Subscribe

    Could not find what you are looking for? Search Java Examples




    Feel Tired? Read Jokes & Inspirational Stories, Play Games!