• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Search Engine Optimization and Internet Marketing
  • Earning with Website and Blogging
  • Software and Hardware
  • New and Happening
  • Other
  • Sponsored Post
You are here: Home / Programming and Scripting / Java: How To Implement ungetc in Java

Java: How To Implement ungetc in Java

December 18, 2013 by Sze Hau Leave a Comment

If you are a C or C++ programmer, you may familiar withe ungetc function in C. ungetc function is a very simple but useful function especially when your program need to read characters from a FILE stream for data parsing. You may encounter cases where you need to unread the character so that you can use that character when you pass in the FILE reference into another function.

From C manual for ungetc:

A character is virtually put back into an input stream, decreasing its internal file position as if a previous getc operation was undone.

This character may or may not be the one read from the stream in the preceding input operation. In any case, the next character retrieved from stream is the character passed to this function, independently of the original one.

Notice though, that this only affects further input operations on that stream, and not the content of the physical file associated with it, which is not modified by any calls to this function.

This is my simple version of ungetc in Java, UInputStream. I use an InputStream as the base class so that you can work with other input stream object like FileInputStream. UInputStream will override the read() function from InputStream and an additional function to undone the read.

import java.io.IOException;
import java.io.InputStream;

public class UInputStream extends InputStream {
    private InputStream uIn;
    private boolean uPushedBack;
    private int uPushedBackVal;

    public UInputStream(InputStream in) {
        uIn = in;
        uPushedBack = false;
        uPushedBackVal = -1;
    }

    @Override
    public int read() throws IOException {
        if (uPushedBack) {
            uPushedBack = false;
            return uPushedBackVal;
        }
        return uPushedBackVal = uIn.read();
    }

    public void unread() {
        if (uPushedBack)
            throw new IllegalStateException("Already pushed back a byte");
        uPushedBack = true;
    }
}

Here is how you use the UInputStream

InputStream in = new UInputStream(/*other stream object*/);
int c;
while ((c = in.read()) != -1) {
    if (c == 'A') {
        in.unread()
        doSomethingStartsWithA(in);
    }
}

More from my site

  • Java: Use ByteBuffer As InputStreamJava: Use ByteBuffer As InputStream
  • Java: Continuously Read Data From FileChannel Without MappedByteBufferJava: Continuously Read Data From FileChannel Without MappedByteBuffer
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • C# – Loop all files in directory and it’s sub-directoriesC# – Loop all files in directory and it’s sub-directories

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: C#, Java, ungetc

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Search Engine Optimization

Short URL: Top 5 Websites That Provide Free Short URL Service

December 10, 2009 By Sze Hau Leave a Comment

Google Chromium OS

November 23, 2009 By Sze Hau Leave a Comment

WordPress Plugin: Official StatCounter Plugin

November 13, 2009 By Sze Hau 1 Comment

Make Money With Website And Blogging

Wordpress

WordPress: How To Change Admin Username

September 23, 2015 By Sze Hau Leave a Comment

WordPress: Transfer Your Blogs To Another Hosting With Minimum Downtime

February 3, 2014 By Sze Hau Leave a Comment

Virtualmin: How To Install Virtualmin in CentOS

August 26, 2013 By Sze Hau Leave a Comment

Software and Hardware

iOS 5.1 Unable to Update

March 13, 2012 By Sze Hau Leave a Comment

Blog Network

  • All Gadget Latest Gadget Reviews and News
  • Blog Portal A place where bloggers gather
  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments
  • Vista Talks Software News, Software Updates, Tips And Tricks

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2021 · Magazine Pro Theme on Genesis Framework · WordPress · Log in