The Artima Developer Community
Sponsored Link

Java Buzz Forum
When Autoboxing Attacks!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michael Cote

Posts: 10306
Nickname: bushwald
Registered: May, 2003

Cote is a programmer in Austin, Texas.
When Autoboxing Attacks! Posted: Jun 30, 2005 10:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Michael Cote.
Original Post: When Autoboxing Attacks!
Feed Title: Cote's Weblog: Coding, Austin, etc.
Feed URL: https://cote.io/feed/
Feed Description: Using Java to get to the ideal state.
Latest Java Buzz Posts
Latest Java Buzz Posts by Michael Cote
Latest Posts From Cote's Weblog: Coding, Austin, etc.

Advertisement

We started using J2SE 5.0 (or "1.5" as everyone calls it) a little while ago. Here's a fun little autoboxing typo-bug I just did:

// in some class that implements java.io.Externalizable

  private long startDate_;
  private int intervalMinutes_;

  public void writeExternal( ObjectOutput out )
    throws IOException
    {
    out.writeLong( serialVersionUID );
    out.writeObject( startDate_ );
    out.writeInt( intervalMinutes_ );
    }

  public void readExternal( ObjectInput in )
    throws IOException
    {
    in.readLong();

    startDate_ = in.readLong();
    intervalMinutes_ = in.readInt();
    }

// When run, throws:
Caused by: java.io.EOFException
 at java.io.DataInputStream.readFully(DataInputStream.java:178)
 at java.io.DataInputStream.readLong(DataInputStream.java:380)
 at java.io.ObjectInputStream$BlockDataInputStream.readLong(ObjectInputStream.java:2748)

Yowz! WTF, dude? The problem is with that out.writeObject( startDate_ );. It's autoboxing the long into a java.lang.Long, and then shitting the bed when it's reading it back in as a long.

Luckily, it's an easy enough to fix to do out.writeObject( startDate_ ); -> out.writeLong( startDate_ );.

Read: When Autoboxing Attacks!

Topic: XMLStarlet on OS X Previous Topic   Next Topic Topic: Sample chapter for the JBoss notebook posted

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use