Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Should be simple.

  1. #1
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default [SOLVED] Should be simple.

    Im trying to make genuine use of the "seek" method in RandomFileAccess,

    Would the getPosistion within this class be a valid marker for the current posistion? (As you may as already geuss Im here because It isnt)
    package org.name.cache;
     
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
     
    public class EXPOutStream extends DataOutputStream {
     
    	private int posistion;
     
    	public EXPOutStream(OutputStream out) {
    		super(out);
    	}
     
    	public int getPosistion() {
    		return posistion;
    	}
     
    	@Override
    	public synchronized void write(byte[] b, int off, int len) throws IOException {
    		super.write(b, off, len);
    		setPosistion(getPosistion() + len);
    	}
     
    	public void setPosistion(int posistion) {
    		this.posistion = posistion;
    	}
     
    }

    Anyone know what the correct method would be?
    Last edited by Time; June 22nd, 2010 at 02:31 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Should be simple.

    Wouldn't it be something more like this?

    setPosistion(off + len);

    As I understand the off variable is the offset of where to start your writing, or?

    // Json

  3. #3
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Should be simple.

    Yes the off is offset from within the actual byte array you begin off, the len is the actual length you base off of that,

    The method within the actual DatainputStream would look simular to this:
           for (int i = 0; i <  length; i++) {
                write(buffer[offset + i]);
            }

    so the l is still the amount of actual bytes being written

    So me saying
    setPosistion(getPosistion() + len);

    Is basically adding the current position plus the number of bytes being written (len)

    Just not understanding whats wrong with it.

  4. #4
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Should be simple.

    Haha fixed didn't relize there was a field "written" within the datainputstream.

    Thanks JSon for your attempt anyway.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Should be simple.

    Yeah I had a hunch there'd already be data in there so you'd have to offset that. Good stuff

    // Json

Similar Threads

  1. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 04:43 PM
  2. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 02:47 PM
  3. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  4. help on simple accumulator
    By Khalon in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 11:39 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM