1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.network.serializing.serializers;
34 
35 import com.jme3.export.Savable;
36 import com.jme3.export.binary.BinaryExporter;
37 import com.jme3.export.binary.BinaryImporter;
38 import com.jme3.network.serializing.Serializer;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.OutputStream;
42 import java.nio.ByteBuffer;
43 
44 public class SavableSerializer extends Serializer {
45 
46     private BinaryExporter exporter = new BinaryExporter();
47     private BinaryImporter importer = new BinaryImporter();
48 
49     private static class BufferOutputStream extends OutputStream {
50 
51         ByteBuffer output;
52 
BufferOutputStream(ByteBuffer output)53         public BufferOutputStream(ByteBuffer output){
54             this.output = output;
55         }
56 
57         @Override
write(int b)58         public void write(int b) throws IOException {
59             output.put( (byte) b );
60         }
61 
62         @Override
write(byte[] b)63         public void write(byte[] b){
64             output.put(b);
65         }
66 
67         @Override
write(byte[] b, int off, int len)68         public void write(byte[] b, int off, int len){
69             output.put(b, off, len);
70         }
71     }
72 
73     private static class BufferInputStream extends InputStream {
74 
75         ByteBuffer input;
76 
BufferInputStream(ByteBuffer input)77         public BufferInputStream(ByteBuffer input){
78             this.input = input;
79         }
80 
81         @Override
read()82         public int read() throws IOException {
83             if (input.remaining() == 0)
84                 return -1;
85             else
86                 return input.get() & 0xff;
87         }
88 
89         @Override
read(byte[] b)90         public int read(byte[] b){
91             return read(b, 0, b.length);
92         }
93 
94         @Override
read(byte[] b, int off, int len)95         public int read(byte[] b, int off, int len){
96             int toRead = len > input.remaining() ? input.remaining() : len;
97             input.get(b, off, len);
98             return toRead;
99         }
100 
101     }
102 
103     @Override
104     @SuppressWarnings("unchecked")
readObject(ByteBuffer data, Class<T> c)105     public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
106         BufferInputStream in = new BufferInputStream(data);
107         Savable s = importer.load(in);
108         in.close();
109         return (T) s;
110     }
111 
112     @Override
writeObject(ByteBuffer buffer, Object object)113     public void writeObject(ByteBuffer buffer, Object object) throws IOException {
114         Savable s = (Savable) object;
115         BufferOutputStream out = new BufferOutputStream(buffer);
116         exporter.save(s, out);
117         out.close();
118     }
119 
120 }
121