์•ˆ๋…•ํ•˜์„ธ์š” ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค. 


์˜ค๋Š˜์€ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค.



1. Object to byte[]


ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
  out = new ObjectOutputStream(bos);   
  out.writeObject(yourObject);
  byte[] yourBytes = bos.toByteArray();
  ...
} finally {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    bos.close();
  } catch (IOException ex) {
    // ignore close exception
  }
}


2. byte[] to Object


ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
  in = new ObjectInputStream(bis);
  Object o = in.readObject(); 
  ...
} finally {
  try {
    bis.close();
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    if (in != null) {
      in.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
}


๊ธฐ๋ณธ์ ์ธ ์ž๋ฐ”๋ฅผ ์ด์šฉํ•˜๋ฉด ์ €๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ๋˜๋Š”๋ฐ ์•„ํŒŒ์น˜ ์œ ํ‹ธ์„ ์ด์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ApacheUtils ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.



To Serialize:

byte[] data = SerializationUtils.serialize(yourObject);

To Deserialize:

YourObject yourObject = (YourObject) SerializationUtils.deserialize(byte[] data)
๋‹ฌ๋ž‘ ํ•œ์ค„๋กœ Object -> byte[] ๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ์—ญ์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋„ค์š”.

์ฐธ ๊ฐ„๋‹จํ•˜์ฅฌ์ž‰~?

์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค.

์ฆํ”„ํ•˜์„ธ์š”~