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.sql.Timestamp;
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.SqlTimestampCodec;
031
032 /**
033 * @author Franck WOLFF
034 */
035 public class SqlTimestampCodecImpl extends AbstractStandardCodec<Timestamp> implements SqlTimestampCodec {
036
037 public int getObjectType() {
038 return JMF_SQL_TIMESTAMP;
039 }
040
041 public Class<?> getObjectClass() {
042 return Timestamp.class;
043 }
044
045 public void encode(OutputContext ctx, Timestamp v) throws IOException {
046 final OutputStream os = ctx.getOutputStream();
047
048 os.write(JMF_SQL_TIMESTAMP);
049
050 long t = v.getTime();
051
052 os.write((int)(t >> 56));
053 os.write((int)(t >> 48));
054 os.write((int)(t >> 40));
055 os.write((int)(t >> 32));
056 os.write((int)(t >> 24));
057 os.write((int)(t >> 16));
058 os.write((int)(t >> 8));
059 os.write((int)t);
060 }
061
062 public Timestamp decode(InputContext ctx, int parameterizedJmfType) throws IOException {
063 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
064
065 if (jmfType != JMF_SQL_TIMESTAMP)
066 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
067
068 long t = ((long)ctx.safeRead()) << 56;
069 t |= ((long)ctx.safeRead()) << 48;
070 t |= ((long)ctx.safeRead()) << 40;
071 t |= ((long)ctx.safeRead()) << 32;
072 t |= ((long)ctx.safeRead()) << 24;
073 t |= ((long)ctx.safeRead()) << 16;
074 t |= ((long)ctx.safeRead()) << 8;
075 t |= ctx.safeRead();
076 return new Timestamp(t);
077 }
078
079 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
080 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
081
082 switch (jmfType) {
083 case JMF_SQL_TIMESTAMP:
084 ctx.indentPrintLn(Timestamp.class.getName() + ": " + decode(ctx, parameterizedJmfType));
085 break;
086 default:
087 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
088 }
089 }
090 }