Serialize

μ§λ ¬ν™”λž€ ?

μ§λ ¬ν™”λŠ” Java μ‹œμŠ€ν…œ λ‚΄λΆ€μ—μ„œ μ‚¬μš©λ˜λŠ” Object λ˜λŠ” Data λ₯Ό μ™ΈλΆ€μ˜ Java 기반 μ‹œμŠ€ν…œμ—μ„œλ„ μ‚¬μš©ν•  수 μžˆλ„λ‘ byte ν˜•νƒœμ˜ λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜λŠ” 기술

μ΄λŠ” JVM (Java Virtual Machine) 이 λ©”λͺ¨λ¦¬μ— 상주 (Heap λ˜λŠ” Stack) λ˜μ–΄ μžˆλŠ” Object 데이터λ₯Ό byte ν˜•νƒœλ‘œ λ³€ν™˜ν•œλ‹€.

역직렬화

byte μ½”λ“œλ‘œ λ˜μ–΄ μžˆλŠ” Data λ₯Ό μ›λž˜λŒ€λ‘œ Object ν˜Ήμ€ Data 둜 λ³€ν™˜ν•˜λŠ” κΈ°μˆ μ„ λ§ν•œλ‹€.

μ΄λŠ” λ‹€μ‹œ 말해 μ§λ ¬ν™”λœ 데이터λ₯Ό Object 둜 λ³€ν™˜ν•΄μ„œ JVM 내뢀에 상주 μ‹œν‚€λŠ”κ²ƒ 이닀.

직렬화 ν•˜κΈ°

Serializable μ΄λΌλŠ” 마컀 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†ν•˜μ—¬ κ΅¬ν˜„ν•œλ‹€.

직렬화 쑰건

java.io.Serializable μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ ν˜•νƒœλ‘œ μž‘μ—…ν•œλ‹€.

public void serializable() {
  Member member = new Member("κΉ€λͺ…석", 10);

  byte[] serializable;

  String serialStr = null;

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {

      oos.writeObject(member);

      serializable = baos.toByteArray();

      serialStr = Base64.getEncoder().encodeToString(serializable);

      System.out.println(serialStr);

  } catch (Exception e) {
      e.printStackTrace();
  }

  byte[] seraialDecode = Base64.getDecoder().decode(serialStr);

  ByteArrayInputStream bais = new ByteArrayInputStream(seraialDecode);

  try (ObjectInputStream ois = new ObjectInputStream(bais)) {

      Object objMember = ois.readObject();

      Member om = (Member) objMember;

      System.out.println(om.toString());

  } catch (Exception e) {
      e.printStackTrace();
  }
}

static class Member implements Serializable {
  private static String name;
  private static int age;

  public Member(String name, int age) {
      this.name = name;
      this.age = age;
  }

  @Override
  public String toString() {
      return String.format("이름 : %s, λ‚˜μ΄ : %d", name, age);
  }
}

transient ν‚€μ›Œλ“œ

transient ν‚€μ›Œλ“œλ₯Ό 뢙이 λ³€μˆ˜λŠ” 직렬화 λŒ€μƒμ—μ„œ μ œμ™Έλœλ‹€.

데이터λ₯Ό λ””μŠ€ν¬μ— μ €μž₯ν•˜κ±°λ‚˜ 디비에 μ €μž₯ν• λ•Œ Http Request λ₯Ό 톡해 ν†΅μ‹ ν•˜λŠ” 경우 민감정보 (κ°œμΈμ •λ³΄λ₯Ό ν¬ν•¨ν•œ λΉ„λ°€λ²ˆν˜Έμ™€ 같은 정보듀) 을 μ œμ™Έν•˜κ³  μ‹ΆμœΌλ©΄ transient 을 λΆ™μ—¬μ„œ μ‚¬μš©ν•˜λ©΄ λœλ‹€.

JPA λͺ¨λΈμ˜ 경우 @transient μ–΄λ…Έν…Œμ΄μ…˜μ„ 톡해 DB κ°’κ³Ό 맀핑 μ‹œν‚€μ§€ μ•ŠλŠ”λ‹€.

public class User implements Serializable {
    private static final long serialVersionUID = 100000000000000001L;

    private String username;
    private transient String passWord;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}
public class UserTest {
    @Test
    public void transientField() throws IOException, ClassNotFoundException {
        final User user = new User("rrest", "1234");

        final FileOutputStream fos = new FileOutputStream("/tmp/user");
        final ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(user);
        oos.flush();
        oos.close();
        fos.close();

        final FileInputStream fis = new FileInputStream("/tmp/user");
        final ObjectInputStream ois = new 
    }
}