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
021 package org.granite.messaging.jmf.codec.std.impl;
022
023 import java.io.IOException;
024 import java.io.OutputStream;
025 import java.util.Date;
026
027 import org.granite.messaging.jmf.DumpContext;
028 import org.granite.messaging.jmf.InputContext;
029 import org.granite.messaging.jmf.JMFEncodingException;
030 import org.granite.messaging.jmf.OutputContext;
031 import org.granite.messaging.jmf.codec.std.DateCodec;
032
033 /**
034 * @author Franck WOLFF
035 */
036 public class DateCodecImpl extends AbstractStandardCodec<Date> implements DateCodec {
037
038 public int getObjectType() {
039 return JMF_DATE;
040 }
041
042 public Class<?> getObjectClass() {
043 return Date.class;
044 }
045
046 public void encode(OutputContext ctx, Date v) throws IOException {
047 if (v == null)
048 throw new JMFEncodingException("Date value must be null");
049
050 final OutputStream os = ctx.getOutputStream();
051
052 os.write(JMF_DATE);
053
054 long t = v.getTime();
055
056 os.write((int)(t >> 56));
057 os.write((int)(t >> 48));
058 os.write((int)(t >> 40));
059 os.write((int)(t >> 32));
060 os.write((int)(t >> 24));
061 os.write((int)(t >> 16));
062 os.write((int)(t >> 8));
063 os.write((int)t);
064 }
065
066 public Date decode(InputContext ctx, int parameterizedJmfType) throws IOException {
067 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
068
069 if (jmfType != JMF_DATE)
070 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
071
072 long t = ((long)ctx.safeRead()) << 56;
073 t |= ((long)ctx.safeRead()) << 48;
074 t |= ((long)ctx.safeRead()) << 40;
075 t |= ((long)ctx.safeRead()) << 32;
076 t |= ((long)ctx.safeRead()) << 24;
077 t |= ((long)ctx.safeRead()) << 16;
078 t |= ((long)ctx.safeRead()) << 8;
079 t |= ctx.safeRead();
080 return new Date(t);
081 }
082
083 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
084 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
085
086 switch (jmfType) {
087 case JMF_DATE:
088 ctx.indentPrintLn(Date.class.getName() + ": " + decode(ctx, parameterizedJmfType));
089 break;
090 default:
091 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
092 }
093 }
094 }