2010-04-20 2 views
1

바이트 스트림으로 직렬화되어 데이터베이스에 보관 된 XML 문서가 있습니다. 나는 바이트 스트림을 다시 얻지 만 어떻게 그것을 XML 파일로 다시 변환 할 수 있습니까?ByteArrayInputStream을 xml 파일로 변환

+0

바이트 스트림의 형식은 무엇입니까? XML이 아닌가? –

답변

0

방법은 몇 []와 백 바이트 앞뒤로 변환된다. 물론 객체는 문자열 일 수 있습니다.

public static Object byteArrayToObject(byte[] data) 
    { 
     Object retObject = null; 
     if (data != null) 
     { 
     ByteArrayInputStream bis = null; 
     ObjectInputStream ois = null; 
     try 
     { 
      bis = new ByteArrayInputStream(data); 
      ois = new ObjectInputStream(bis); 

      retObject = ois.readObject(); 
     } 
     catch(StreamCorruptedException e) 
     { 
      e.printStackTrace(System.out); 
     } 
     catch(OptionalDataException e) 
     { 
      e.printStackTrace(System.out); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(System.out); 
     } 
     catch(ClassNotFoundException e) 
     { 
      e.printStackTrace(System.out); 
     } 
     finally 
     { 
      try 
      { 
       bis.close(); 
      } 
      catch(IOException ex) 
      { 
       ex.printStackTrace(System.out); 
      } 

      try 
      { 
       ois.close(); 
      } 
      catch(IOException ex) 
      { 
       ex.printStackTrace(System.out); 
      } 
     } 
     } 
     return retObject; 
    } 

    public static byte[] objectToByteArray(Object anObject) 
    { 
     byte[] results = null; 
     if (anObject != null) 
     { 
     // create a byte stream to hold the encoded object 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

     try 
     { 
      // create a stream to write the object 
      ObjectOutputStream ostrm = new ObjectOutputStream(bytes); 

      // write the object 
      ostrm.writeObject(anObject); 

      // ensure that the entire object is written 
      ostrm.flush(); 

      results = bytes.toByteArray(); 

      try 
      { 
       ostrm.close(); 
      } 
      catch (IOException e) 
      { 
      } 

      try 
      { 
       bytes.close(); 
      } 
      catch (IOException e) 
      { 
      } 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(System.out); 
     } 
     } 
     return results; 
    } 

P.S. 다락방에서 파낸이 오래된 코드 - 현대화해야합니다.