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    import java.util.ArrayList;
027    
028    import org.granite.messaging.jmf.CodecRegistry;
029    import org.granite.messaging.jmf.DumpContext;
030    import org.granite.messaging.jmf.InputContext;
031    import org.granite.messaging.jmf.JMFEncodingException;
032    import org.granite.messaging.jmf.OutputContext;
033    import org.granite.messaging.jmf.codec.StandardCodec;
034    import org.granite.messaging.jmf.codec.std.ArrayListCodec;
035    
036    /**
037     * @author Franck WOLFF
038     */
039    public class ArrayListCodecImpl extends AbstractIntegerStringCodec<ArrayList<?>> implements ArrayListCodec {
040    
041            public int getObjectType() {
042                    return JMF_ARRAY_LIST;
043            }
044    
045            public Class<?> getObjectClass() {
046                    return ArrayList.class;
047            }
048    
049            public void encode(OutputContext ctx, ArrayList<?> v) throws IOException {
050                    final OutputStream os = ctx.getOutputStream();
051                    
052                    int indexOfStoredObject = ctx.indexOfStoredObjects(v);
053                    if (indexOfStoredObject >= 0) {
054                            IntegerComponents ics = intComponents(indexOfStoredObject);
055                            os.write(0x80 | (ics.length << 5) | JMF_ARRAY_LIST);
056                            writeIntData(ctx, ics);
057                    }
058                    else {
059                            ctx.addToStoredObjects(v);
060                            
061                            Object[] snapshot = v.toArray();
062                            
063                            IntegerComponents ics = intComponents(snapshot.length);
064                            os.write((ics.length << 5) | JMF_ARRAY_LIST);
065                            writeIntData(ctx, ics);
066    
067                            for (Object element : snapshot)
068                                    ctx.writeObject(element);
069                    }
070            }
071    
072            public ArrayList<?> decode(InputContext ctx, int parameterizedJmfType) throws IOException, ClassNotFoundException {
073                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
074                    
075                    if (jmfType != JMF_ARRAY_LIST)
076                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
077                    
078                    int indexOrLength = readIntData(ctx, (parameterizedJmfType >> 5) & 0x03, false);
079                    if ((parameterizedJmfType & 0x80) != 0)
080                            return (ArrayList<?>)ctx.getSharedObject(indexOrLength);
081    
082                    ArrayList<Object> v = new ArrayList<Object>(indexOrLength);
083                    ctx.addSharedObject(v);
084                    
085                    for (int index = 0; index < indexOrLength; index++)
086                            v.add(index, ctx.readObject());
087                                    
088                    return v;
089            }
090    
091            public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
092                    final CodecRegistry codecRegistry = ctx.getSharedContext().getCodecRegistry();
093                    
094                    int jmfType = codecRegistry.extractJmfType(parameterizedJmfType);
095                    
096                    if (jmfType != JMF_ARRAY_LIST)
097                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
098                    
099                    int indexOrLength = readIntData(ctx, (parameterizedJmfType >> 5) & 0x03, false);
100                    if ((parameterizedJmfType & 0x80) != 0) {
101                            String v = (String)ctx.getSharedObject(indexOrLength);
102                            ctx.indentPrintLn("<" + v + "@" + indexOrLength + ">");
103                            return;
104                    }
105    
106                    String v = ArrayList.class.getName() + "[" + indexOrLength + "]";
107                    int indexOfStoredObject = ctx.addSharedObject(v);
108                    ctx.indentPrintLn(v + "@" + indexOfStoredObject + " {");
109                    ctx.incrIndent(1);
110                    
111                    for (int index = 0; index < indexOrLength; index++) {
112                            parameterizedJmfType = ctx.safeRead();
113                            jmfType = codecRegistry.extractJmfType(parameterizedJmfType);
114                            StandardCodec<?> codec = codecRegistry.getCodec(jmfType);
115                            
116                            if (codec == null)
117                                    throw new JMFEncodingException("No codec for JMF type: " + jmfType);
118                            
119                            codec.dump(ctx, parameterizedJmfType);
120    
121                    }
122                            
123                    ctx.incrIndent(-1);
124                    ctx.indentPrintLn("}");
125            }
126    }