Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Help with Readable Interface

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Help with Readable Interface

    Java Code:
    package myPackage;
    
    import java.io.IOException;
    import java.nio.CharBuffer;
    import java.util.Scanner;
    
    public class RandomWords implements Readable
    {
    	private static final char[] capitals = "JASON".toCharArray();
    	
    	private int count;
    	
    	public RandomWords(int count) 
    	{
    		this.count = count;
    	}
    	
    	@Override
    	public int read(CharBuffer cb) throws IOException 
    	{
    		count = count-1;
    
    		cb.append(capitals[count]);
    		
    		if(count == 0)
    			return -1;
    		
    		return 0;
    	}
    	
    	public static void main(String[] args)
    	{
    		Scanner s = new Scanner(new RandomWords(5));
    		
    		while(s.hasNext())
    			System.out.println(s.next());
    	}
    }
    Output: Nojas

    Hi everyone,

    My question is:
    Can someone please tell me when the read() method is called ?
    Can you also help with the charbuffer cb ? When is it created ? I do not see anyone creating a charbuffer cb here but it is used ?

    Thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    Read the source of Scanner.java and you'll be able to find it.

    -- see what the constructor does with the Readable argument
    -- see how it is used and when/where its read(...) method is called.
    -- see where the CharBuffer comes from

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    This is all I can find and it does not answer my questions:


    A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    Read the source, not the API. You'll find the sources in src.zip in your JDK installation folder.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    In the jdk folder I only see a folder named api

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    You're looking in the wrong folder. The JDK installation folder has about 5 or 6 folders and 7 or 8 files, none of which is named "api"

    Search your computer for "src.zip"

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    I definetly do not have that file. :(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Readable Interface

    I didn't get the source for 1.7 either with my download, but I know that you can get the source for Java 1.6 here:

    Java SE Downloads

    Search for "Java SE 6 JDK Source Code" on this page.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    Sorry about that, and thanks for the info.

    Oracle claims the src.zip ships with JDK7
    JDK File Structure

    There's an alternative download here:
    jdk7src – Alternative to JDK src.zip with all publicly available source files

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    Ok I found the Scanner.java in src-jdk.zip and opened it with notepad but it is impossible to read this.

    Am I trying to read something wrong ?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    Notepad doesn't correctly render newlines without carriage returns (\n without \r). Use a decent text or code editor.

    What IDE do you use?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    Eclipse

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Help with Readable Interface

    Try opening the source in Eclipse's code editor then.

    I use NetBeans, which can open files, in read-only mode, directly from a zip archive without the need to extract the files first. Maybe Eclipse has something like that -- wait for Fubarable or one of the Eclipse users to happen by.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with Readable Interface

    Is this the Javadoc mentioned here?

    This here:

    Scanner (Java Platform SE 6)

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2011, 08:45 AM
  2. Make a readable book for mobile phones?
    By xmenus in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-11-2010, 06:12 PM
  3. Readable Serialization
    By falven in forum Advanced Java
    Replies: 3
    Last Post: 05-31-2010, 01:57 AM
  4. ObjectInputStream wait for readable objects
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 06-08-2009, 03:43 AM
  5. Convertin a .DAT file into readable format
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •