All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.serialization.standard.io.surrogates.DateSerializationSurrogate Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * @(#)DateSerializationSurrogate.java	1.0
 *
 * Created on September 18, 2008, 12:59 PM
 *
 * Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
 * "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
 * to license terms.
 */
package com.alachisoft.ncache.serialization.standard.io.surrogates;

import com.alachisoft.ncache.runtime.util.NCDateTime;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.surrogates.*;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * DateSerializationSurrogate is responsible for writing and reading
 * instances of Date class.
 *
 * @version 1.0, September 18, 2008
 */
public class DateSerializationSurrogate
        extends SerializationSurrogateBase
        implements SerializationSurrogate, BuiltinSerializationSurrogate {

    protected long DIFF = 621355968000000000L;

    /**
     * Creates a new instance of NxBooleanSerializationSurrogate
     */
    public DateSerializationSurrogate() {
        super(Date.class);
    }

    /**
     * Read and return an object. The class that implements this interface
     * defines where the object is "read" from.
     *
     * @param input the stream to read data from in order to restore the object.
     * @return the object read from the stream
     * @throws java.lang.ClassNotFoundException If the class of a serialized
     *                                          object cannot be found.
     * @throws NCacheIOException                If any of the usual Input/Output
     *                                          related exceptions occur.
     */
    public Object readObject(NCacheObjectInput input)
            throws NCacheInstantiationException, NCacheIOException {
        try {
            // Many problems with Java and .net Date - DateTime calculations mostly of leap year calculations
            // For more information use the internet
            //Date date = new Date(( (input.readLong() - DIFF ) / 10000) - (TimeZone.getDefault().getRawOffset()));

            return (new NCDateTime(input.readLong())).getDate();
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        }
    }

    /**
     * Write an object to the underlying storage or stream.  The
     * class that implements this interface defines how the object is
     * written.
     *
     * @param output the stream to write the object to.
     * @param graph  the object to write .
     * @throws NCacheIOException Any of the usual Input/Output related exceptions.
     */
    public void writeObject(NCacheObjectOutput output, Object graph)
            throws NCacheIOException {
        try {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(Calendar.MILLISECOND, 0);
            c.setTime((Date) graph);
            //long ticks = ((date.getTime() + TimeZone.getDefault().getRawOffset()) * 10000) + DIFF;
            NCDateTime ncdt = new NCDateTime(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND), c.get(Calendar.MILLISECOND));
            output.writeLong(ncdt.getTicks());
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        } catch (IllegalArgumentException arg) {
            throw new NCacheIOException(arg);
        }
    }

    public void skipObject(NCacheObjectInput input) throws NCacheInstantiationException, NCacheIOException {
        try {
            input.skipLong();
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy