001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.messaging.jmf.codec.std.impl;
023    
024    import java.io.IOException;
025    import java.io.OutputStream;
026    
027    import org.granite.messaging.jmf.DumpContext;
028    import org.granite.messaging.jmf.InputContext;
029    import org.granite.messaging.jmf.OutputContext;
030    import org.granite.messaging.jmf.codec.std.IntegerCodec;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public class IntegerCodecImpl extends AbstractIntegerStringCodec<Integer> implements IntegerCodec {
036    
037            public int getObjectType() {
038                    return JMF_INTEGER_OBJECT;
039            }
040    
041            public Class<?> getObjectClass() {
042                    return Integer.class;
043            }
044    
045            public int getPrimitiveType() {
046                    return JMF_INTEGER;
047            }
048    
049            public Class<?> getPrimitiveClass() {
050                    return Integer.TYPE;
051            }
052    
053            public void encode(OutputContext ctx, Integer v) throws IOException {
054                    writeIntData(ctx, JMF_INTEGER_OBJECT, v.intValue());
055            }
056            
057            public Integer decode(InputContext ctx, int parameterizedJmfType) throws IOException {
058                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
059                    
060                    if (jmfType != JMF_INTEGER_OBJECT)
061                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
062                    
063                    return Integer.valueOf(readIntData(ctx, parameterizedJmfType));
064            }
065    
066            public void encodePrimitive(OutputContext ctx, int v) throws IOException {
067                    writeIntData(ctx, JMF_INTEGER, v);
068            }
069            
070            public int decodePrimitive(InputContext ctx) throws IOException {
071                    int parameterizedJmfType = ctx.safeRead();
072                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
073                    
074                    if (jmfType != JMF_INTEGER)
075                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
076                    
077                    return readIntData(ctx, parameterizedJmfType);
078            }
079            
080            public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
081                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
082                    
083                    switch (jmfType) {
084                    case JMF_INTEGER:
085                            ctx.indentPrintLn("int: " + readIntData(ctx, parameterizedJmfType));
086                            break;
087                    case JMF_INTEGER_OBJECT:
088                            ctx.indentPrintLn(Integer.class.getName() + ": " + Integer.valueOf(readIntData(ctx, parameterizedJmfType)));
089                            break;
090                    default:
091                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
092                    }
093            }
094            
095            protected void writeIntData(OutputContext ctx, int jmfType, int v) throws IOException {
096                    IntegerComponents ics = intComponents(v);
097                    ctx.getOutputStream().write((ics.sign << 7) | (ics.length << 5) | jmfType);
098                    writeIntData(ctx, ics);
099            }
100            
101            protected int readIntData(InputContext ctx, int parameterizedJmfType) throws IOException {
102                    return readIntData(ctx, (parameterizedJmfType >> 5) & 0x03, (parameterizedJmfType & 0x80) != 0);
103            }
104            
105            public void writeVariableInt(OutputContext ctx, int v) throws IOException {
106    
107                    final OutputStream os = ctx.getOutputStream();
108                    
109                    if (v == Integer.MIN_VALUE)
110                            os.write(0x80); // negative 0.
111                    else {
112                            int sign = 0x00;
113                            if (v < 0) {
114                                    sign = 0x80;
115                                    v = -v;
116                            }
117    
118                            int bytesCount = lengthOfVariableAbsoluteInt(v);
119                            v -= deltaForVariableAbsoluteIntLength(bytesCount);
120                            
121                            switch (bytesCount) {
122                            case 0:
123                                    os.write(sign | v);
124                                    break;
125                            case 1: case 2: case 3:
126                                    os.write(sign | 0x40 | (v >> (bytesCount * 7)));
127                                    for (int i = bytesCount - 1; i > 0; i--)
128                                            os.write(0x80 | (v >> (i * 7)));
129                                    os.write(0x7F & v);
130                                    break;
131                            case 4:
132                                    os.write(sign | 0x40 | (v >> 29));
133                                    os.write(0x80 | (v >> 22));
134                                    os.write(0x80 | (v >> 15));
135                                    os.write(0x80 | (v >> 8));
136                                    os.write(v);
137                                    break;
138                            }
139                    }
140            }
141            
142            public int readVariableInt(InputContext ctx) throws IOException {
143                    int v = ctx.safeRead();
144                    
145                    if (v == 0x80)
146                            v = Integer.MIN_VALUE;
147                    else {
148                            boolean opposite = ((v & 0x80) != 0);
149                            boolean readNext = (v & 0x40) != 0;
150                            
151                            v &= 0x3F;
152                            
153                            if (readNext) {
154                                    int l = 1;
155                                    for (int i = 1; i <= 3; i++) {
156                                            int u = ctx.safeRead();
157                                            v = (v << 7) | (u & 0x7F);
158                                            if ((u & 0x80) == 0) {
159                                                    readNext = false;
160                                                    break;
161                                            }
162                                            l++;
163                                    }
164                                    if (readNext)
165                                            v = (v << 8) | ctx.safeRead();
166                                    v += deltaForVariableAbsoluteIntLength(l);
167                            }
168                            
169                            if (opposite)
170                                    v = -v;
171                    }
172                    
173                    return v;
174            }
175            
176            protected static final int[] VARIABLE_INT_DELTAS = new int[5];
177            static {
178                    VARIABLE_INT_DELTAS[0] = 0;
179                    for (int i = 1; i < VARIABLE_INT_DELTAS.length; i++)
180                            VARIABLE_INT_DELTAS[i] = (VARIABLE_INT_DELTAS[i-1] << 7) | 0x40;
181            }
182            
183            protected static int lengthOfVariableAbsoluteInt(int abs) {
184                    for (int i = 1; i < VARIABLE_INT_DELTAS.length; i++) {
185                            if (abs < VARIABLE_INT_DELTAS[i])
186                                    return (i - 1);
187                    }
188                    return 4;
189            }
190            
191            protected static int deltaForVariableAbsoluteIntLength(int len) {
192                    return VARIABLE_INT_DELTAS[len];
193            }
194    }