Source code: com/k_int/codec/runtime/Integer_codec.java
1 /**
2 *
3 * Integer_codec : Basic integer codec
4 *
5 * @author Ian Ibbotson ( ibbo@k-int.com )
6 * @version $Id: Integer_codec.java,v 1.2 2000/12/23 10:44:33 ianibbo Exp $
7 *
8 * Copyright: Copyright (C) 2000, Knowledge Integration Ltd.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1 of
13 * the license, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite
23 * 330, Boston, MA 02111-1307, USA.
24 *
25 *
26 */
27
28 package com.k_int.codec.runtime;
29
30 import java.math.BigInteger;
31
32 public class Integer_codec extends base_codec
33 {
34 private static Integer_codec me = null;
35
36 public static Integer_codec getCodec()
37 {
38 if ( null == me )
39 me = new Integer_codec();
40
41 return me;
42 }
43
44 public Object serialize(SerializationManager sm,
45 Object type_instance,
46 boolean is_optional,
47 String type_name) throws java.io.IOException
48 {
49 BigInteger retval = (BigInteger)type_instance;
50
51 boolean is_constructed = false;
52
53 if ( sm.getDirection() == SerializationManager.DIRECTION_ENCODE )
54 {
55 if ( null != retval )
56 {
57 sm.implicit_settag(SerializationManager.UNIVERSAL, SerializationManager.INTEGER);
58
59 int len = sm.tag_codec(false);
60
61 if ( len == 0 )
62 throw new java.io.IOException("Error encoding length");
63
64 retval = sm.integer_codec(retval, is_constructed);
65 }
66 }
67 else if ( sm.getDirection() == SerializationManager.DIRECTION_DECODE )
68 {
69 sm.implicit_settag(SerializationManager.UNIVERSAL, SerializationManager.INTEGER);
70 int len = sm.tag_codec(false);
71 if ( len >= 0 )
72 {
73 retval = sm.integer_codec(retval, is_constructed);
74 }
75 }
76
77 if ( ( null == retval ) && ( ! is_optional ) )
78 throw new java.io.IOException("Missing mandatory member: "+type_name);
79
80 return (Object)retval;
81 }
82 }