Results 1 to 3 of 3
  1. #1
    Wassa is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default StreamCorruptedException and Casting troubles

    It seems that every now and then, my project has been throwing exceptions when my client has received an object from the client.

    I have no idea what is causing it as the stack trace leads from the objectinputstream.receive() into the java API.

    The exceptions I am getting are either 'StreamCorruptedExceptions' or Casting errors where it attempts to cast one object from within the tableUpdate onto another (see class below), but I have no code which is doing this and I think it is related to the corruptedstream as they both started cropping up at the same time.

    Does anyone know what may be causing it?

    Example Exception:
    Java Code:
    java.io.StreamCorruptedException: invalid handle value: 7371007E
    
            at java.io.ObjectInputStream.readHandle(ObjectInputStream.java:1433)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1309)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
            at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
            at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
            at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
            at common.network.IOSocket.receive(IOSocket.java:59)
            at client.execute.ClientThread.run(ClientThread.java:34)
            at java.lang.Thread.run(Thread.java:619)
    The Object Which Is Being Sent: (all objects within are also Serializable)
    Java Code:
    package server.table.updates;
    
    import common.cards.Card;
    import java.io.Serializable;
    import server.game.variables.BlindLevel;
    import server.game.Game;
    import server.game.variables.PokerBreed;
    import server.playablehand.HandStage;
    import server.table.player.Player;
    
    public class TableUpdate implements Serializable {
        private Player[] players;
        private Card communityCards[] = new Card[]{ null, null, null, null, null};
        private int dealerPosition;
        private BlindLevel blinds;
        private int id;
        private int pot = -1;
        private String name;
        private PokerBreed breed;
        private int playerTurn = -1;
        private UpdateType type;
        private String displayText = "";
        
        /**
         * Creates a new instance of TableUpdate
         */
        public TableUpdate(Game game, UpdateType type) {
            this.type = type;
            init(game);
            
        }
        
        private void init(Game game){
            this.breed = game.getState().getBreed();
            this.id = game.getId();
            this.name = game.getName().toString();
            this.blinds = game.getState().getTable().getBlinds();
            this.dealerPosition = game.getState().getTable().getDealerPosition();
            this.players = game.getState().getTable().getPlayers();
            
            
            
            // if the game is not paused there will not be a hand being played
            if (!this.type.equals(UpdateType.SHOWPAUSEDGAME)){
                this.communityCards = game.getHand().getCommunityCards();
                this.playerTurn = game.getHand().getHandPlayer().getPlayerTurn();
                this.pot = game.getHand().getPot();
            }
        }
        
        
        public void stripAllCards(){
            for (int i=0; i<players.length; i++){
                players[i].setHoleCards(null);
            }
        }
        
        public void stripCardsForPlayer(int playerSeat){
            for (int i=0; i<players.length; i++){
                if (i!=playerSeat){
                    players[i].setHoleCards(null);
                }
            }
        }
        
        public Player[] getPlayers() {
            return players;
        }
        
        public int getDealerPosition() {
            return dealerPosition;
        }
        
        public BlindLevel getBlinds() {
            return blinds;
        }
        
        public Card[] getCommunityCards(){
            return this.communityCards;
        }
        
        public int getId() {
            return id;
        }
        
        public int getPot(){
            return this.pot;
        }
        
        public String getName() {
            return name;
        }
        
        public UpdateType getType(){
            return this.type;
        }
        
        public PokerBreed getBreed(){
            return this.breed;
        }
        
        public int getPlayerTurn(){
            return this.playerTurn;
        }
        
    }
    Before it is sent, it undergoes a deep copy: (not sure if its relevant or not)
    Java Code:
      private TableUpdate deepCopy(TableUpdate update){
            try {
                
                // converts the object to a byte array
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(update);
                oos.close();
                byte[] byteArr = baos.toByteArray();
                
                // converts the byte array back to an object
                ByteArrayInputStream bais = new ByteArrayInputStream(byteArr);
                ObjectInputStream ois = new ObjectInputStream(bais);
                update = (TableUpdate) ois.readObject();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return update;
        }

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Essentially, the problem is that the client is getting to a point where it "gets the wrong bit of data" to what it is expecting. Without seeing all of your code, it's difficult to say more, but possible causes include:

    - when writing the data, you're sending a buffer at a time, but incorrectly missing cases where not all of the buffer is written to the stream
    - possibly a synchronization error if two threads are either reading or writing to the same stream for some reason
    - some other logic in what object/data you're deciding to read or write to the stream when

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    What Neil said is correct. To put it in more simple words, the client is expecting very specific input from the socket, where the input represents objects. If the client expects a certain object, and it gets *anything* that does not represent that object, the you get an exception.

    Neil listed several possible reasons for this.

    Looking at your code, I notice that you did not include the sending or the receiving code. Therefore, it's impossible to say what might be causing the problem.

    My guess is that the sender and the receiver are not in complete agreement about what is being sent. Once in a while something is added or left out, which results in the error.

Similar Threads

  1. Replies: 8
    Last Post: 11-05-2010, 11:26 PM
  2. Replies: 3
    Last Post: 01-29-2010, 07:53 AM
  3. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:31 PM
  4. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  5. java.io.StreamCorruptedException
    By elizabeth in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 07:45 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
  •