본문 바로가기

Language/Java

Java에서 직렬화(Serializable)가 불가능한 경우


1. 직렬화가 불가능한 경우
   1) 직렬화가 불가능한 객체를 포함한 경우
   2) 하위 클래스는 직렬화를 구현했지만 상위 클래스에서는 직렬화가 구현되지 않은 상태에서 
       상위 클래스의 생성자에 매개변수가 있는 경우

2. 직렬화 불가능한 클래스들
   1) 이벤트 어댑터
   2) 이미지 필터
   3) AWT 클래스
   4) beans
   5) Socket
   6) URLConnection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
 
/**
 * Serializable은 구현했지만 직렬화할 수 없는 객체를 포함한 경우
 */
 
public class MyNetwork implements Serializable {
    private Socket socket;      //직렬화 불가능한 객체
    public MyNetwork() throws IOException{
        socket = new Socket();
    }
}

  [ 수정 소스 ]

1
2
3
4
5
6
7
8
9
10
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
 
public class MyNetwork implements Serializable {
    private transient Socket socket;        //transient 사용
    public MyNetwork() throws IOException{
        socket = new Socket();
    }
}


3. 상속구조에서 직렬화할 수 없는 상황
    (엄밀하게 말하면 직렬화는 가능하지만 역직렬화가 불가능한 상태)
   1) 상위 클래스의 생성자에 매개변수가 있는 생성자만 존재
   2) 상위 클래스는 Serializable을 구현하지 않은 상태
   3) 하위 클래스는 상위 클래스를 상속받은 뒤 Serializable을 구현한 상태

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
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * 상속구조에서 직렬화할 수 없는 상황
 */
 
class Parent extends Object{
    public Parent(String str){
        //....
    }
}
 
class Child extends Parent implements Serializable{
    public Child(String str) throws IOException{
        super(str);
    }
}
 
public class SerialConsMain {
    public static void main(String[] args)
        throws IOException, ClassNotFoundException{
         
        Child c = new Child("test");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(bout);
        oout.writeObject(c);
        oout.close();
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream oin = new ObjectInputStream(bin);
        Object o = oin.readObject();
    }
}

 [ 수정 소스 ]

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
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/*
 * < 에러 해결책 >
 * 상위 클래스에 implements Serializable을 붙여준다.
 */
 
class Parent extends Object implements Serializable{
    public Parent(String str){
        //....
    }
}
 
class Child extends Parent implements Serializable{
    public Child(String str) throws IOException{
        super(str);
    }
}
 
public class SerialConsMain {
    public static void main(String[] args)
        throws IOException, ClassNotFoundException{
         
        Child c = new Child("test");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(bout);
        oout.writeObject(c);
        oout.close();
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream oin = new ObjectInputStream(bin);
        Object o = oin.readObject();
    }
}


  [참고] www.jabook.com

<script src="http://goppa.tistory.com/plugin/CallBack_bootstrapper?&src=http://s1.daumcdn.net/cfs.tistory/v/0/blog/plugins/CallBack/callback&id=168&callbackId=goppatistorycom1682418&destDocId=callbacknestgoppatistorycom1682418&host=http://goppa.tistory.com&float=left&random=812"></script>

'Language > Java' 카테고리의 다른 글

OXM(Object XML Mapping) [XML 바인딩 기술들...]  (0) 2014.02.27
Java Doc  (0) 2014.02.27
Java enum  (0) 2014.02.27
Java Varargs (가변인자) 사용법  (1) 2014.02.27
Dom4j를 이용한 XML Parsing  (0) 2014.02.27