A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Java bites! Er, bytes. Yeah...

Hybrid View

  1. #1
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784

    Java bites! Er, bytes. Yeah...

    Gargh!

    Trying to get a basic little app to work in Java - simple: read in file, perform some operations (display, reverse, rename, encrypt). I've got it all working (except for the encrypt part) while reading the file into a CharBuffer, but I'm trying to convert to a ByteBuffer to do the encription...

    I've got it loading properly, but the display function always kicks out "0 - Exit" at the end when casting the bytes to chars; it's causing some real problems when reversing the order of bytes in the reverse function...

    Anyone know how to troubleshoot this? Is it a matter of eliminating an eof character from the inputstream, or testing the last bytes for a specific value (which, for the life of me, I cannot think of).

    If anyone is bored enough and home on a Friday night, my code follows:

    public void showFile() throws IOException{
    // display contents of file
    FileChannel inChannel = (new FileInputStream(fileName)).getChannel();

    originalBytes.clear();
    // ByteBuffer obj declared earlier

    originalBytes.allocate((int)inChannel.size());

    while(inChannel.read(originalBytes) != -1){
    // read bytes into buffer
    }

    originalBytes.flip();

    inChannel.close();

    // now, convert bytes to chars and output

    while (originalBytes.hasRemaining()){
    System.out.print( (char) (originalBytes.get() & 0xFF) );
    }

    }

    public void reverseFile(){
    // reverse the file
    originalBytes.rewind();

    // declare 2nd buffer
    reverseBytes.allocate(originalBytes.limit());

    // read first buffer into second in reverse order

    for (int i = originalBytes.limit(); i <=0; i--){
    reverseBytes.put(i, originalBytes.get());
    }

    try{
    FileChannel outChannel = (new FileOutputStream(fileName)).getChannel();

    outChannel.write(reverseBytes);

    outChannel.close();

    originalBytes.clear();
    System.out.println("The file has been reversed. Please select option 2 to view the results.\n");
    } catch (IOException ioe){
    System.out.println("Write to file failed.");
    }

    }

    Any help appreciated!
    Hush child. japangreg can do what he wants. - PAlexC
    That was Zen - this is Tao.

  2. #2
    --===00OO00===--
    Join Date
    Jan 2007
    Location
    Noobisle...
    Posts
    62
    :O I think this be the wrong spot. You should know by now.

    If it isn't, the rules are written wrong.
    "For an assassin he...he is pretty nice." ~The Darkness that Comes Before

    The Catalyst Corp.

  3. #3
    Retired SCORM Guru PAlexC's Avatar
    Join Date
    Nov 2000
    Location
    NJ
    Posts
    1,387
    Hush child. japangreg can do what he wants.
    "What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
    ...and now I have tape all over my face.

  4. #4
    pablo cruisin' hanratty21's Avatar
    Join Date
    Mar 2002
    Location
    on the lam
    Posts
    2,275
    Quote Originally Posted by PAlexC
    Hush child. japangreg can do what he wants.
    </zing!>
    "Why does it hurt when I pee?" -- F. Zappa |

  5. #5
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784
    Quote Originally Posted by PAlexC
    Hush child. japangreg can do what he wants.
    That's footer material right there.
    Hush child. japangreg can do what he wants. - PAlexC
    That was Zen - this is Tao.

  6. #6
    --===00OO00===--
    Join Date
    Jan 2007
    Location
    Noobisle...
    Posts
    62
    :P I know, noone will pay it attention though.
    "For an assassin he...he is pretty nice." ~The Darkness that Comes Before

    The Catalyst Corp.

  7. #7
    Wait- what now? tidenburg's Avatar
    Join Date
    Dec 2005
    Posts
    1,471
    As a wise gerbick once said.
    use a lower case p
    "I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."

  8. #8
    Senior Member Genesis F5's Avatar
    Join Date
    Jan 2002
    Location
    Unallocated memory
    Posts
    1,845
    One more reason to not learn Java. I'm fine with OOP, but that looks ridiculous. I hope the NetBeans IDE has code hinting.

  9. #9
    Senior Member
    Join Date
    Jul 2005
    Posts
    692
    Just roll with vb.net or Ruby on Rails. Depending on the environment you're programming for. You'll most likely want to kill yourself less.

  10. #10
    Did you ever figure this out? It seems like you would want to use originalBytes.getChar() instead of originalBytes.get()

  11. #11
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784
    Quote Originally Posted by yasunobu13
    Did you ever figure this out? It seems like you would want to use originalBytes.getChar() instead of originalBytes.get()
    So it would seem, but Java hates me and makes me jump through hoops.

    <sheepish grin>
    Figured out the "0 - Exit" problem, at least: I forgot to put a line break between the output and my menu (which starts with 0 - Exit, 1 - Select File, etc.) This has got to be one of the worst "You've got to be f-ing kidding me" moments... I just spent 2 days searching the net for my own damn error...
    </sheepish grin>

    Now all I have to do is fix the reverse function to not write past the limit of the buffer and I'm good... thanks to all for the help, anyway.

    *edit*

    Got it - and the encrypt function, too. You know, once you remove blinding stupidity from the equation, this isn't all that hard... =^_^=
    Last edited by japangreg; 08-16-2007 at 04:08 PM.
    Hush child. japangreg can do what he wants. - PAlexC
    That was Zen - this is Tao.

  12. #12
    Databarnak atRax's Avatar
    Join Date
    Dec 2000
    Location
    Zurich, Switzerland
    Posts
    258
    ... woooo .. see that UFO?
    I ask you all to concentrate really hard on the freedom of all being. Its hard not to be very angry it is impossible We have to focus this confusion frustration helplessness feeling into a creative outlet Anger can spawn such amazing creativity through Street art Free art to teach each other know each other a language our evolution Go ahead and break some dumb rules

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center