Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ObjectInputStream vs DataInputStream  (Read 3530 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-11-04 16:30:07 »

Can someone describe the difference between those 2 different types, because in my code, I used ObjectInputStream and the entire program just hung there. However, when i used DataInputStream, it didn't hang, but i can no longer use the readObject method. Can someone offer maybe some suggestions for a solution?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-11-04 16:55:00 »

I think the javadoc for them makes it very clear what the difference is, as does your experience using them.  They're not interchangeable.  You may need to go in with a debugger or sprinkle logging statements around your code to determine where things are hanging.
Offline cubemaster21
« Reply #2 - Posted 2012-11-04 16:56:32 »

I had determined that where I create the ObjectInputStream was where the program was hanging, but a had no idea why.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-11-04 17:12:56 »

Paste the code that's hanging (use [ code ] tags) and we can take a look.
Offline cubemaster21
« Reply #4 - Posted 2012-11-04 17:14:54 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
Socket connection = server.accept();
      System.out.println(connection);
      connections.add(connection);
      System.out.println("Connection recieved from " + connection.getInetAddress().getHostName());
     
      ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
      System.out.println("input established");
     
      ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
      System.out.println("output established");
      output.flush();

it gets to the line where it creates 'input' and it doesn't go any further than that.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-11-04 17:50:23 »

So you get the "Connection received" message, but not the "input established" message?  That is curious ... to my knowledge there's nothing about constructing an OIS that should be inherently be blocking, only reading it.  Maybe Socket.getInputStream() is doing it, try breaking that out into another line with its own debug line.
Offline cubemaster21
« Reply #6 - Posted 2012-11-04 17:54:54 »

Ok, i did what you said, and it doesn't seem to be a problem with Socket.getInputStream(). This is what i have now, with the line broken up.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
                Socket connection = server.accept();
      System.out.println(connection);
      connections.add(connection);
      System.out.println("Connection recieved from " + connection.getInetAddress().getHostName());
     
      InputStream is = connection.getInputStream();
      System.out.println("InputStream created");
     
     
      ObjectInputStream input = new ObjectInputStream(is);
      System.out.println("input established");
     
      ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
      System.out.println("output established");
      output.flush();

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Online Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2012-11-04 18:09:12 »

Just like GZipInputStream, ObjectInputSteam does I/O in the constructor (which is aweful).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    public ObjectInputStream(InputStream in) throws IOException {
        verifySubclass();
        bin = new BlockDataInputStream(in);
        handles = new HandleTable(10);
        vlist = new ValidationList();
        enableOverride = false;
       readStreamHeader();
        bin.setBlockDataMode(true);
    }

    public GZIPInputStream(InputStream in, int size) throws IOException {
        super(in, new Inflater(true), size);
        usesDefaultInflater = true;
       readHeader(in);
    }


There are two awkward solutions...
  • Creating an ObjectInputStream wrapper, that defers the I/O to the first read
  • Creating an ObjectInputStream on its own thread.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #8 - Posted 2012-11-04 18:10:50 »

Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

I had just ass-u-me'd that java.io was actually, well, sane.  Anyway, as long as the other end sends an object eventually, you should be fine.

Offline cubemaster21
« Reply #9 - Posted 2012-11-04 18:13:07 »

Well, i've just set it up so that it creates the Output first and flushes it, AND THEN creates the input and it seems  to be working just fine. Thanks for all the help guys.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-11-04 18:17:13 »

Something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
class LazyObjectInputStream extends DataInputStream
{
   private final DataInputStream dis;
   private ObjectInputStream ois;

   public LazyObjectInputStream(DataInputStream dis) {
      this.dis = dis;
      this.ois = null;
   }

   private boolean inited;
   private void ensureInited() throws IOException {
      if(inited) {
         return;
      }
      inited = true;

      ios = new ObjectInputStream(dis);
   }

   public int read() throws IOException {
      this.ensureInited();
      return ios.read();
   }

   public int read(byte[] b) throws IOException {
      this.ensureInited();
      return ios.read(b);
   }

   public int read(byte[] b, int off, int len) throws IOException {
      this.ensureInited();
      return ios.read(b, off, len);
   }

   public Object readObject() throws IOException {
      this.ensureInited();
      return ios.readObject();
   }

   public void close() throws IOException {
      if(ios==null) {
         dis.close();
      }
      else {
         ois.close();
      }
   }
}

because you really, really want to do I/O on your own terms. Cranky

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!