Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marauroa/net/InputSerializer.java


1   /* $Id: InputSerializer.java,v 1.3 2003/12/08 01:08:30 arianne_rpg Exp $ */
2   /***************************************************************************
3    *                      (C) Copyright 2003 - Marauroa                      *
4    ***************************************************************************
5    ***************************************************************************
6    *                                                                         *
7    *   This program is free software; you can redistribute it and/or modify  *
8    *   it under the terms of the GNU General Public License as published by  *
9    *   the Free Software Foundation; either version 2 of the License, or     *
10   *   (at your option) any later version.                                   *
11   *                                                                         *
12   ***************************************************************************/
13  package marauroa.net;
14  
15  import java.io.*;
16  
17  /** InputSerializer is used to serialize classes that implement the Serializable
18   *  interface from a InputStream.
19   */
20  public class InputSerializer
21    {
22    private InputStream in;
23    
24    /** Constructor that pass the InputStream to the serializer
25        @param in the InputStream */
26    public InputSerializer(InputStream in)
27      {
28      this.in=in;
29      }
30    
31    /** This method serialize an object that implements the interface Serializable
32        allowing to implement this behaviour in several classes
33        @param obj the object were we will serialize the data
34        @return the object serialized, just for interface coherence
35        @throws java.io.IOException if there is an IO error
36        @throws java.lang.ClassNotFoundException if the class to serialize doesn't exist. */
37      public Object readObject(marauroa.net.Serializable obj) throws IOException, java.lang.ClassNotFoundException
38      {
39      obj.readObject(this);
40      return obj;
41      }
42      
43    /** This method read a byte from the Serializer
44        @return the byte serialized
45        @throws java.io.IOException if there is an IO error
46        @throws java.lang.ClassNotFoundException if the class to serialize doesn't exist. */
47    public byte readByte() throws IOException, java.lang.ClassNotFoundException
48      {
49      int result=in.read();
50      if(result<0)
51        {
52        throw new IOException();
53        }
54      return (byte)result;
55      }
56      
57    /** This method read a byte array from the Serializer
58        @return the byte array serialized
59        @throws java.io.IOException if there is an IO error
60        @throws java.lang.ClassNotFoundException if the class to serialize doesn't exist. */
61    public byte[] readByteArray() throws IOException, java.lang.ClassNotFoundException
62      {
63      int size=readInt();
64      byte[] buffer=new byte[size];
65      
66      in.read(buffer);
67      
68      return buffer;
69      }
70  
71    /** This method read a short from the Serializer
72        @return the short serialized
73        @throws java.io.IOException if there is an IO error
74        @throws java.lang.ClassNotFoundException
75          if the class to serialize doesn't exist. */
76    public short readShort() throws IOException, java.lang.ClassNotFoundException
77      {
78      byte[] data=new byte[2];
79      
80      int result=in.read(data);
81      if(result<0)
82        {
83        throw new IOException();
84        }
85        
86      result=data[0]&0xFF;
87      result+=(data[1]&0xFF) << 8;
88      return (short)result;
89      }
90  
91    /** This method read a int from the Serializer
92        @return the int serialized
93        @throws java.io.IOException if there is an IO error
94        @throws java.lang.ClassNotFoundException
95          if the class to serialize doesn't exist. */
96    public int readInt() throws IOException, java.lang.ClassNotFoundException
97      {
98      byte[] data=new byte[4];
99      int result=in.read(data);
100     if(result<0)
101       {
102       throw new IOException();
103       }
104       
105     result=data[0]&0xFF;
106     result+=(data[1]&0xFF) << 8;
107     result+=(data[2]&0xFF) << 16;
108     result+=(data[3]&0xFF) << 24;
109     return result;
110     }
111   
112   /** This method read a String from the Serializer
113       @return the String serialized
114       @throws java.io.IOException if there is an IO error
115       @throws java.lang.ClassNotFoundException
116         if the class to serialize doesn't exist. */
117   public String readString() throws IOException, java.lang.ClassNotFoundException,UnsupportedEncodingException
118     {
119     return new String(readByteArray(),"UTF-8");
120     }
121 
122   /** This method read a String array from the Serializer
123       @return the String array serialized
124       @throws java.io.IOException if there is an IO error
125       @throws java.lang.ClassNotFoundException
126         if the class to serialize doesn't exist. */
127   public String[] readStringArray() throws IOException, java.lang.ClassNotFoundException
128     {
129     int size=readInt();
130     
131     String[] buffer=new String[size];
132     for(int i=0;i<size;i++)
133       {
134       buffer[i]=readString();
135       }
136     
137     return buffer;
138     }
139   };