Commits

Tobias Westerblom committed f7c5292

Changed from throwing checked to unchecked exceptions. (#8)

Comments (0)

Files changed (2)

core/src/main/java/com/cubeia/firebase/io/StyxJsonSerializer.java

 		return doObjectMapperCall(new ObjectMapperCall<String>() {
 			
 			@Override
-			public String call(ObjectMapper mapper) throws JsonProcessingException, IOException {
-				return mapper.writeValueAsString(obj);
+			public String call(ObjectMapper mapper) {
+				try {
+					return mapper.writeValueAsString(obj);
+				} catch (Exception e) {
+					throw new RuntimeException("error marshalling to json", e);
+				} 
 			}
 		});
 	}
 		return doObjectMapperCall(new ObjectMapperCall<String>() {
 			
 			@Override
-			public String call(ObjectMapper mapper) throws JsonProcessingException, IOException {
-				return mapper.writeValueAsString(obj);
+			public String call(ObjectMapper mapper) {
+				try {
+					return mapper.writeValueAsString(obj);
+				} catch (Exception e) {
+					throw new RuntimeException("error marshalling to json", e);
+				}
 			}
 		});
 	}
 		return doObjectMapperCall(new ObjectMapperCall<ProtocolObject>() {
 			
 			@Override
-			public ProtocolObject call(ObjectMapper mapper) throws JsonProcessingException, IOException {
-				return mapper.readValue(json, ProtocolObject.class);
+			public ProtocolObject call(ObjectMapper mapper) {
+				try {
+					return mapper.readValue(json, ProtocolObject.class);
+				} catch (Exception e) {
+					throw new RuntimeException("error unmarshalling from json", e);
+				}
 			}
 		});
 	}
 		return doObjectMapperCall(new ObjectMapperCall<List<ProtocolObject>>() {
 			
 			@Override
-			public List<ProtocolObject> call(ObjectMapper mapper) throws JsonProcessingException, IOException {
-				return mapper.readValue(json, new TypeReference<List<ProtocolObject>>() { });
+			public List<ProtocolObject> call(ObjectMapper mapper) {
+				try {
+					return mapper.readValue(json, new TypeReference<List<ProtocolObject>>() { });
+				} catch (Exception e) {
+					throw new RuntimeException("error unmarshalling from json", e);
+				}		
 			}
 		});
 	}
 		module.addDeserializer(ProtocolObject.class, new StdDeserializer<ProtocolObject>(ProtocolObject.class) {
 
 			@Override
-			public ProtocolObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+			public ProtocolObject deserialize(JsonParser jp, DeserializationContext ctxt) {
 				ObjectMapper mapper = (ObjectMapper) jp.getCodec();  
-			    ObjectNode root = (ObjectNode) mapper.readTree(jp); 
-			    /*
-			     * Get the class id and the refer to the factory to map the
-			     * concrete class.
-			     */
-			    int classId = root.get(CLASS_ID_PROPERTY).asInt();
-			    Class<? extends ProtocolObject> cl = factory.create(classId).getClass();
-			    return mapper.readValue(root, cl);
+			    ObjectNode root;
+				try {
+					root = (ObjectNode) mapper.readTree(jp);
+					/*
+					 * Get the class id and the refer to the factory to map the
+					 * concrete class.
+					 */
+					int classId = root.get(CLASS_ID_PROPERTY).asInt();
+					Class<? extends ProtocolObject> cl = factory.create(classId).getClass();
+					return mapper.readValue(root, cl);
+				} catch (Exception e) {
+					throw new RuntimeException("error unmarshalling from json", e);
+				} 
 			}
 		}); 
 		// Add mix-in to mimic a real property for the class ID
 	}
 	
 	private interface ObjectMapperCall<T> {
-		
 		public T call(ObjectMapper mapper) throws JsonProcessingException, JsonMappingException, IOException;
-		
 	}
 }

core/src/main/java/com/cubeia/firebase/io/StyxSerializer.java

     /**
      * Unpack a byte sequence into a concrete ProtocolObject.
      */
-    public ProtocolObject unpack(ByteBuffer inBuffer) throws IOException {
+    public ProtocolObject unpack(ByteBuffer inBuffer) {
     	Integer payloadLength = inBuffer.getInt();
     	
     	// Styx by default uses length exclusive from the length header
         
         int classId = BinaryData.asUnsigned(inBuffer.get());
         ProtocolObject po = factory.create(classId);
-        po.load(new PacketInputStream(inBuffer));
+        try {
+			po.load(new PacketInputStream(inBuffer));
+		} catch (IOException e) {
+			throw new RuntimeException("error unpacking buffer", e);
+		}
 
         return po;
     }
 
-    public byte[] packArray(ProtocolObject obj) throws IOException {
+    public byte[] packArray(ProtocolObject obj) {
     	if (obj.classId() > 255 || obj.classId() < 0) {
     		throw new IllegalArgumentException("classId " + obj.classId() + " is out of range. Legal values are inside a signed byte.");
     	}
     	return packed;
     }
 
-	public ByteBuffer pack(ProtocolObject obj) throws IOException {
+	public ByteBuffer pack(ProtocolObject obj) {
     	return ByteBuffer.wrap(packArray(obj));
-    	
-        
-        
-       /* ByteBuffer buf = ByteBuffer.allocate(4+1+packed.length); // Size+type+payload
-        // buf.putShort((short)(1+2+packed.length));
-        buf.putInt(4+1+packed.length); // Size+type+payload
-        buf.put((byte)obj.classId());
-        buf.put(packed);
-        buf.rewind();*/
-        
-//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
-//        DataOutputStream dos = new DataOutputStream(bos);
-//        dos.writeShort(1 + 2 + packed.length);
-//        dos.writeByte(obj.classId());
-//        dos.write(packed, 0, packed.length);
-//        dos.flush();
-        
-        
-        // return buf;
     }
 	
     private void writeClassidHeader(ProtocolObject obj, byte[] bytes) {
      * This method returns a byte array with 5 bytes free at the start (written
      * as zeroes) for the packet header (size+classid).
      */
-    private byte[] packObject(ProtocolObject obj) throws IOException {
+    private byte[] packObject(ProtocolObject obj) {
         ByteArrayOutputStream bos = new ByteArrayOutputStream(37);
         for (int i = 0; i < 5; i++) {
         	// write header bytes...
         return bos.toByteArray();
     }
 
-	private void packObject(ProtocolObject obj, ByteArrayOutputStream bos) throws IOException {
+	private void packObject(ProtocolObject obj, ByteArrayOutputStream bos) {
 		DataOutputStream      dos = new DataOutputStream(bos);
         PacketOutputStream    pos = new PacketOutputStream(dos);
-        obj.save(pos);
-        dos.flush();
+        try {
+			obj.save(pos);
+			dos.flush();
+		} catch (IOException e) {
+			throw new RuntimeException("error packing object", e);
+		}
 	}
 
     private final ObjectFactory factory;