Tuesday Oct 23, 2012

Using Coherence API to get POF bytes

Someone raised the question on how to use the Coherence API to get the bytes of an object in POF (Portable Object Format) programatically. So I came up with this small code that shows the very cool API simple usage :-)

   SimplePofContext spc = new SimplePofContext();
   spc.registerUserType(0, User.class, new UserSerializer()); 
   // consider UserSerializer as an implementation of PofSerializer
            
   User u = new User();
   u.setId(21);
   u.setName("Some Name");
   u.setEmail("some.name@domain.com");
            
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   DataOutput dataOutput = new DataOutputStream(baos);
   BufferOutput bufferOutput = new WrapperBufferOutput(dataOutput);
   spc.serialize(bufferOutput, u);
            
   byte[] byteArray = baos.toByteArray();
   System.out.println(Arrays.toString(byteArray));

 Easy, isn't?

About


Bruno has been having fun working with Java since 2000 and now helps Oracle on sharing the technology accross all Latin America. Also plays videogames, does trekking and loves beer.

Follow me on Twitter! @brunoborges

Search

Archives
« October 2012 »
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
27
28
29
30
31
   
       
Today