001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2013 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.messaging.jmf.codec.std.impl;
022
023import java.io.IOException;
024
025import org.granite.messaging.jmf.DumpContext;
026import org.granite.messaging.jmf.InputContext;
027import org.granite.messaging.jmf.JMFEncodingException;
028import org.granite.messaging.jmf.OutputContext;
029import org.granite.messaging.jmf.codec.std.NullCodec;
030
031/**
032 * @author Franck WOLFF
033 */
034public class NullCodecImpl extends AbstractStandardCodec<Object> implements NullCodec {
035
036        public int getObjectType() {
037                return JMF_NULL;
038        }
039
040        public Class<?> getObjectClass() {
041                return null;
042        }
043
044        public void encode(OutputContext ctx, Object v) throws IOException {
045                if (v != null)
046                        throw new JMFEncodingException("Null value must be null");
047                
048                ctx.getOutputStream().write(JMF_NULL);
049        }
050        
051        public Object decode(InputContext ctx, int parameterizedJmfType) throws IOException {
052                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
053                
054                if (jmfType != JMF_NULL)
055                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
056                
057                return null;
058        }
059        
060        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
061                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
062                
063                if (jmfType != JMF_NULL)
064                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
065
066                ctx.indentPrintLn("?: null");
067        }
068}