com.gemstone.gemfire.DataSerializer Maven / Gradle / Ivy
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.gemstone.gemfire.admin.RegionNotFoundException;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.internal.DSCODE;
import com.gemstone.gemfire.internal.HeapDataOutputStream;
import com.gemstone.gemfire.internal.InternalDataSerializer;
import com.gemstone.gemfire.internal.ObjToByteArraySerializer;
import com.gemstone.gemfire.internal.cache.CachedDeserializable;
import com.gemstone.gemfire.internal.cache.EventID;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
import com.gemstone.gemfire.internal.offheap.ByteSource;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.shared.Version;
import com.gemstone.gemfire.pdx.PdxInstance;
import com.gemstone.gnu.trove.THashMap;
import com.gemstone.gnu.trove.TObjectObjectProcedure;
/**
* Provides static helper methods for reading and writing
* non-primitive data when working with a {@link DataSerializable}.
* For instance, classes that implement DataSerializable
* can use the DataSerializer
in their
* toData
and fromData
methods:
*
*
*
*
public class Employee implements DataSerializable {
private int id;
private String name;
private Date birthday;
private Company employer;
public void toData(DataOutput out) throws IOException {
out.writeInt(this.id);
out.writeUTF(this.name);
DataSerializer.writeDate(this.birthday, out);
DataSerializer.writeObject(this.employer, out);
}
public void fromData(DataInput in)
throws IOException, ClassNotFoundException {
this.id = in.readInt();
this.name = in.readUTF();
this.birthday = DataSerializer.readDate(in);
this.employer = (Company) DataSerializer.readObject(in);
}
}
*
*
*
*
* Instances of DataSerializer
are used to data serialize
* objects (such as instances of standard Java classes or third-party
* classes for which the source code is not available) that do not
* implement the DataSerializable
interface.
*
*
*
* The following DataSerializer
data serializes instances
* of Company
. In order for the data serialization
* framework to consult this custom serializer, it must be {@linkplain
* #register(Class) registered} with the framework.
*
*
*
*
public class CompanySerializer extends DataSerializer {
static {
DataSerializer.register(CompanySerializer.class);
}
/**
* May be invoked reflectively if instances of Company are
* distributed to other VMs.
*/
public CompanySerializer() {
}
public Class[] getSupportedClasses() {
return new Class[] { Company.class };
}
public int getId() {
return 42;
}
public boolean toData(Object o, DataOutput out)
throws IOException {
if (o instanceof Company) {
Company company = (Company) o;
out.writeUTF(company.getName());
// Let's assume that Address is java.io.Serializable
Address address = company.getAddress();
writeObject(address, out);
return true;
} else {
return false;
}
}
public Object fromData(DataInput in)
throws IOException, ClassNotFoundException {
String name = in.readUTF();
Address address = (Address) readObject(in);
return new Company(name, address);
}
}
*
*
* Just like {@link Instantiator}s, a DataSerializer
may
* be sent to other members of the distributed system when it is
* {@linkplain #register(Class) registered}. The data serialization
* framework does not require that a DataSerializer
be
* {@link Serializable}, but it does require that it provide a
* {@linkplain #DataSerializer() zero-argument constructor}.
*
* @see #writeObject(Object, DataOutput)
* @see #readObject
*
* @author David Whitlock
* @since 3.5 */
public abstract class DataSerializer {
/** The eventId of this DataSerializer
*/
private EventID eventId;
/** The originator of this DataSerializer
*/
private ClientProxyMembershipID context;
/** If the "DataSerializer.DUMP_SERIALIZED"
system
* property is set, the class names of the objects that are
* serialized by {@link #writeObject(Object, DataOutput)} using standard Java
* serialization are logged to {@linkplain System#out standard out}.
* This aids in determining which classes should implement {@link
* DataSerializable} (or should be special cased by a custom
* DataSerializer
). */
public static final boolean DUMP_SERIALIZED =
Boolean.getBoolean("DataSerializer.DUMP_SERIALIZED");
/** Should verbose debugging information be dumped? */
public static final boolean DEBUG = Boolean.getBoolean("DataSerializer.DEBUG");
protected static final boolean TRACE_SERIALIZABLE =
Boolean.getBoolean("DataSerializer.TRACE_SERIALIZABLE");
/* Used to prevent standard Java serialization when sending data to a non-Java client */
protected static final ThreadLocal DISALLOW_JAVA_SERIALIZATION = new ThreadLocal();
////////////////////// Instance Fields /////////////////////
////////////////////// Static Methods //////////////////////
/**
* Writes an instance of Class
to a
* DataOutput
.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readClass
*/
public static void writeClass(Class> c, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Class " + c);
}
if (c == null || c.isPrimitive()) {
InternalDataSerializer.writePrimitiveClass(c, out);
}
else {
// non-primitive classes have a second CLASS byte
// if readObject/writeObject is called:
// the first CLASS byte indicates it's a Class, the second
// one indicates it's a non-primitive Class
out.writeByte(DSCODE.CLASS);
String cname = c.getName();
cname = swizzleClassNameForWrite(cname);
writeString(cname, out);
}
}
/**
* Writes class name to a DataOutput
. This method will handle a
* null
value and not throw a NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readNonPrimitiveClassName(DataInput)
*/
public static void writeNonPrimitiveClassName(String className, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Class name " + className);
}
writeString(swizzleClassNameForWrite(className), out);
}
/**
* Reads an instance of Class
from a
* DataInput
. The class will be loaded using the
* {@linkplain Thread#getContextClassLoader current content class
* loader}.
* The return value may be null
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class cannot be loaded
*/
public static Class> readClass(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
byte typeCode = in.readByte();
if (typeCode == DSCODE.CLASS) {
String className = readString(in);
className = swizzleClassNameForRead(className);
Class> c = InternalDataSerializer.getCachedClass(className); // fix for bug 41206
return c;
}
else {
return InternalDataSerializer.decodePrimitiveClass(typeCode);
}
}
/**
* For backward compatibility we must swizzle the package of
* some classes that had to be moved when GemFire was open-
* sourced. This preserves backward-compatibility.
*
* @param name the fully qualified class name
* @return the name of the class in this implementation
*/
private static String swizzleClassNameForRead(String name) {
String oldPackage = "com.gemstone.org.jgroups.stack.tcpserver";
String newPackage = "com.gemstone.gemfire.distributed.internal.tcpserver";
String result = name;
if (name.startsWith(oldPackage)) {
result = newPackage + name.substring(oldPackage.length());
}
return result;
}
/**
* For backward compatibility we must swizzle the package of
* some classes that had to be moved when GemFire was open-
* sourced. This preserves backward-compatibility.
*
* @param name the fully qualified class name
* @return the name of the class in this implementation
*/
private static String swizzleClassNameForWrite(String name) {
String oldPackage = "com.gemstone.org.jgroups.stack.tcpserver";
String newPackage = "com.gemstone.gemfire.distributed.internal.tcpserver";
String result = name;
if (name.startsWith(newPackage)) {
result = oldPackage + name.substring(newPackage.length());
}
return result;
}
/**
* Reads name of an instance of Class
from a
* DataInput
.
*
* The return value may be null
.
*
* @throws IOException
* A problem occurs while reading from in
* @see #writeNonPrimitiveClassName(String, DataOutput)
*/
public static String readNonPrimitiveClassName(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
return swizzleClassNameForRead(readString(in));
}
/**
* Writes an instance of Region. A Region is serialized as just a reference
* to a full path only. It will be recreated on the other end by calling
* {@link CacheFactory#getAnyInstance} and then calling
* getRegion
on it.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
*/
public static void writeRegion(Region,?> rgn, DataOutput out)
throws IOException {
writeString((rgn != null) ? rgn.getFullPath() : null, out);
}
/**
* Reads an instance of Region. A Region is serialized as a reference to a
* full path only. It is recreated on the other end by calling
* {@link CacheFactory#getAnyInstance} and then calling
* getRegion
on it.
* The return value may be null
.
*
* @param in the input stream
* @return the Region instance
* @throws com.gemstone.gemfire.cache.CacheClosedException if a cache has not been created or the only
* created one is closed.
* @throws RegionNotFoundException if there is no region by this name
* in the Cache
*/
public static Region readRegion(DataInput in)
throws IOException, ClassNotFoundException {
String fullPath = readString(in);
Region rgn = null;
if (fullPath != null) {
// use getExisting to fix bug 43151
rgn = ((Cache)GemFireCacheImpl.getExisting("Needed cache to find region.")).getRegion(fullPath);
if (rgn == null) {
throw new RegionNotFoundException(LocalizedStrings.DataSerializer_REGION_0_COULD_NOT_BE_FOUND_WHILE_READING_A_DATASERIALIZER_STREAM.toLocalizedString(fullPath));
}
}
return rgn;
}
/**
* Writes an instance of Date
to a
* DataOutput
. Note that even though date
* may be an instance of a subclass of Date
,
* readDate
will always return an instance of
* Date
, not an instance of the subclass. To
* preserve the class type of date
,\
* {@link #writeObject(Object, DataOutput)} should be used for data serialization.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readDate
*/
public static void writeDate(Date date, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Date " + date);
}
long v;
if (date == null) {
v = -1L;
} else {
v = date.getTime();
if (v == -1L) {
throw new IllegalArgumentException("Dates whose getTime returns -1 can not be DataSerialized.");
}
}
out.writeLong(v);
}
/**
* Reads an instance of Date
from a
* DataInput
.
* The return value may be null
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Date readDate(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
long time = in.readLong();
Date date = null;
if (time != -1L) {
date = new Date(time);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Date " + date);
}
return date;
}
/**
* Writes an instance of File
to a
* DataOutput
. Note that even though file
* may be an instance of a subclass of File
,
* readFile
will always return an instance of
* File
, not an instance of the subclass. To
* preserve the class type of file
,
* {@link #writeObject(Object, DataOutput)} should be used for data serialization.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readFile
* @see File#getCanonicalPath
*/
public static void writeFile(File file, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing File " + file);
}
writeString((file != null) ? file.getCanonicalPath() : null, out);
}
/**
* Reads an instance of File
from a
* DataInput
.
* The return value may be null
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static File readFile(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
String s = readString(in);
File file = null;
if (s != null) {
file = new File(s);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read File " + file);
}
return file;
}
/**
* Writes an instance of InetAddress
to a
* DataOutput
. The InetAddress
is data
* serialized by writing its {@link InetAddress#getAddress byte}
* representation to the DataOutput
. {@link
* #readInetAddress} converts the byte
representation
* to an instance of InetAddress
using {@link
* InetAddress#getAddress}. As a result, if address
* is an instance of a user-defined subclass of
* InetAddress
(that is, not an instance of one of the
* subclasses from the java.net
package), its class
* will not be preserved. In order to be able to read an instance
* of the user-defined class, {@link #writeObject(Object, DataOutput)} should be used.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readInetAddress
*/
public static void writeInetAddress(InetAddress address,
DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing InetAddress " + address);
}
writeByteArray((address != null) ? address.getAddress() : null, out);
}
/**
* Reads an instance of InetAddress
from a
* DataInput
.
* The return value may be null
.
*
* @throws IOException
* A problem occurs while reading from in
* or the address read from in
is unknown
*
* @see InetAddress#getAddress
*/
public static InetAddress readInetAddress(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
byte[] address = readByteArray(in);
if (address == null) {
return null;
}
try {
InetAddress addr = InetAddress.getByAddress(address);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read InetAddress " + addr);
}
return addr;
} catch (UnknownHostException ex) {
IOException ex2 = new IOException(LocalizedStrings.DataSerializer_WHILE_READING_AN_INETADDRESS.toLocalizedString());
ex2.initCause(ex);
throw ex2;
}
}
/**
* Writes an instance of String
to a
* DataOutput
.
* This method will handle a
* null
value and not throw a
* NullPointerException
.
* As of 5.7 strings longer than 0xFFFF can be serialized.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readString
*/
public static void writeString(String value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing String \"" + value + "\"");
}
if (value == null) {
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing NULL_STRING");
}
out.writeByte(DSCODE.NULL_STRING);
} else {
// [bruce] writeUTF is expensive - it creates a char[] to fetch
// the string's contents, iterates over the array to compute the
// encoded length, creates a byte[] to hold the encoded bytes,
// iterates over the char[] again to create the encode bytes,
// then writes the bytes. Since we usually deal with ISO-8859-1
// strings, we can accelerate this by accessing chars directly
// with charAt and fill a single-byte buffer. If we run into
// a multibyte char, we revert to using writeUTF()
int len = value.length();
int utfLen = len; // added for bug 40932
for (int i=0; i= 0x0001)) {
// nothing needed
} else if (c > 0x07FF) {
utfLen += 2;
} else {
utfLen += 1;
}
// Note we no longer have an early out when we detect the first
// non-ascii char because we need to compute the utfLen for bug 40932.
// This is not a performance problem because most strings are ascii
// and they never did the early out.
}
boolean writeUTF = utfLen > len;
if (writeUTF) {
if (utfLen > 0xFFFF) {
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf HUGE_STRING of len="+len);
}
out.writeByte(DSCODE.HUGE_STRING);
out.writeInt(len);
out.writeChars(value);
} else {
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing utf STRING of len="+len);
}
out.writeByte(DSCODE.STRING);
out.writeUTF(value);
}
}
else {
if (len > 0xFFFF) {
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing HUGE_STRING_BYTES of len="+len);
}
out.writeByte(DSCODE.HUGE_STRING_BYTES);
out.writeInt(len);
out.writeBytes(value);
} else {
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing STRING_BYTES of len="+len);
}
out.writeByte(DSCODE.STRING_BYTES);
out.writeShort(len);
out.writeBytes(value);
}
}
}
}
/**
* Reads an instance of String
from a
* DataInput
. The return value may be
* null
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeString
*/
public static String readString(DataInput in) throws IOException {
return InternalDataSerializer.readString(in, in.readByte());
}
/**
* Writes an instance of Boolean
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readBoolean
*/
public static void writeBoolean(Boolean value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Boolean " + value);
}
out.writeBoolean(value.booleanValue());
}
/**
* Reads an instance of Boolean
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Boolean readBoolean(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Boolean value = Boolean.valueOf(in.readBoolean());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Boolean " + value);
}
return value;
}
/**
* Writes an instance of Character
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readCharacter
*/
public static void writeCharacter(Character value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Character " + value);
}
out.writeChar(value.charValue());
}
/**
* Reads an instance of Character
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Character readCharacter(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
Character value = Character.valueOf(in.readChar());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Character " + value);
}
return value;
}
/**
* Writes an instance of Byte
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readByte
*/
public static void writeByte(Byte value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Byte " + value);
}
out.writeByte(value.byteValue());
}
/**
* Reads an instance of Byte
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Byte readByte(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Byte value = Byte.valueOf(in.readByte());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Byte " + value);
}
return value;
}
/**
* Writes an instance of Short
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readShort
*/
public static void writeShort(Short value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Short " + value);
}
out.writeShort(value.shortValue());
}
/**
* Reads an instance of Short
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Short readShort(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Short value = Short.valueOf(in.readShort());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Short " + value);
}
return value;
}
/**
* Writes an instance of Integer
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readInteger
*/
public static void writeInteger(Integer value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Integer " + value);
}
out.writeInt(value.intValue());
}
/**
* Reads an instance of Integer
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Integer readInteger(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Integer value = Integer.valueOf(in.readInt());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Integer " + value);
}
return value;
}
/**
* Writes an instance of Long
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readLong
*/
public static void writeLong(Long value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Long " + value);
}
out.writeLong(value.longValue());
}
/**
* Reads an instance of Long
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Long readLong(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Long value = Long.valueOf(in.readLong());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Long " + value);
}
return value;
}
/**
* Writes an instance of Float
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readFloat
*/
public static void writeFloat(Float value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Float " + value);
}
out.writeFloat(value.floatValue());
}
/**
* Reads an instance of Float
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Float readFloat(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Float value = Float.valueOf(in.readFloat());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Float " + value);
}
return value;
}
/**
* Writes an instance of Double
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
* @throws NullPointerException if value is null.
*
* @see #readDouble
*/
public static void writeDouble(Double value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Double " + value);
}
out.writeDouble(value.doubleValue());
}
/**
* Reads an instance of Double
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static Double readDouble(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
Double value = Double.valueOf(in.readDouble());
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Double " + value);
}
return value;
}
/**
* Writes a primitive boolean
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeBoolean
* @since 5.1
*/
public static void writePrimitiveBoolean(boolean value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Boolean " + value);
}
out.writeBoolean(value);
}
/**
* Reads a primitive boolean
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readBoolean
* @since 5.1
*/
public static boolean readPrimitiveBoolean(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
boolean value = in.readBoolean();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Boolean " + value);
}
return value;
}
/**
* Writes a primitive byte
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeByte
* @since 5.1
*/
public static void writePrimitiveByte(byte value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Byte " + value);
}
out.writeByte(value);
}
/**
* Reads a primitive byte
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readByte
* @since 5.1
*/
public static byte readPrimitiveByte(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
byte value = in.readByte();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Byte " + value);
}
return value;
}
/**
* Writes a primitive char
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeChar
* @since 5.1
*/
public static void writePrimitiveChar(char value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Char " + value);
}
out.writeChar(value);
}
/**
* Reads a primitive char
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readChar
* @since 5.1
*/
public static char readPrimitiveChar(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
char value = in.readChar();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Char " + value);
}
return value;
}
/**
* Writes a primitive short
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeShort
* @since 5.1
*/
public static void writePrimitiveShort(short value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Short " + value);
}
out.writeShort(value);
}
/**
* Reads a primitive short
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readShort
* @since 5.1
*/
public static short readPrimitiveShort(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
short value = in.readShort();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Short " + value);
}
return value;
}
/**
* Writes a primitive int
as an unsigned byte to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeByte
* @see DataInput#readUnsignedByte
* @since 5.1
*/
public static void writeUnsignedByte(int value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Unsigned Byte " + value);
}
out.writeByte(value);
}
/**
* Reads a primitive int
as an unsigned byte from a
* DataInput
using {@link DataInput#readUnsignedByte}.
*
* @throws IOException
* A problem occurs while reading from in
* @since 5.1
*/
public static int readUnsignedByte(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
int value = in.readUnsignedByte();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Unsigned Byte " + value);
}
return value;
}
/**
* Writes a primitive int
as an unsigned short to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeShort
* @see DataInput#readUnsignedShort
* @since 5.1
*/
public static void writeUnsignedShort(int value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Unsigned Short " + value);
}
out.writeShort(value);
}
/**
* Reads a primitive int
as an unsigned short from a
* DataInput
using {@link DataInput#readUnsignedShort}.
*
* @throws IOException
* A problem occurs while reading from in
* @since 5.1
*/
public static int readUnsignedShort(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
int value = in.readUnsignedShort();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Unsigned Short " + value);
}
return value;
}
/**
* Writes a primitive int
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeInt
*/
public static void writePrimitiveInt(int value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Integer " + value);
}
out.writeInt(value);
}
/**
* Reads a primitive int
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readInt
* @since 5.1
*/
public static int readPrimitiveInt(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
int value = in.readInt();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Integer " + value);
}
return value;
}
/**
* Writes a primitive long
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeLong
* @since 5.1
*/
public static void writePrimitiveLong(long value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Long " + value);
}
out.writeLong(value);
}
/**
* Reads a primitive long
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readLong
* @since 5.1
*/
public static long readPrimitiveLong(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
long value = in.readLong();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Long " + value);
}
return value;
}
/**
* Writes a primitive float
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeFloat
* @since 5.1
*/
public static void writePrimitiveFloat(float value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Float " + value);
}
out.writeFloat(value);
}
/**
* Reads a primitive float
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readFloat
* @since 5.1
*/
public static float readPrimitiveFloat(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
float value = in.readFloat();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Float " + value);
}
return value;
}
/**
* Writes a primtive double
to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see DataOutput#writeDouble
* @since 5.1
*/
public static void writePrimitiveDouble(double value, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Double " + value);
}
out.writeDouble(value);
}
/**
* Reads a primitive double
from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @see DataInput#readDouble
* @since 5.1
*/
public static double readPrimitiveDouble(DataInput in) throws IOException {
InternalDataSerializer.checkIn(in);
double value = in.readDouble();
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Double " + value);
}
return value;
}
/**
* Writes an array of byte
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readByteArray
*/
public static void writeByteArray(byte[] array, DataOutput out)
throws IOException {
int len = 0;
if (array != null) {
len = array.length;
}
writeByteArray(array, len, out);
}
/**
* Writes the first len
elements
* of an array of byte
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @param len the actual number of entries to write. If len is greater
* than then length of the array then the entire array is written.
* @throws IOException
* A problem occurs while writing to out
*
* @see #readByteArray
*/
public static void writeByteArray(byte[] array, int len, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length = len; // to avoid warnings about parameter assignment
if (array == null) {
length = -1;
} else {
if (length > array.length) {
length = array.length;
}
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing byte array of length " + length);
}
if (length > 0) {
out.write(array, 0, length);
}
}
/**
* Serialize the given object obj
into a byte array
* using {@link #writeObject(Object, DataOutput)} and then writes the byte array
* to the given data output out
in the same format
* {@link #writeByteArray(byte[], DataOutput)} does.
* This method will serialize a
* null
obj and not throw a
* NullPointerException
.
*
* @param obj the object to serialize and write
* @param out the data output to write the byte array to
* @throws IllegalArgumentException
* if a problem occurs while serialize obj
* @throws IOException
* if a problem occurs while writing to out
*
* @see #readByteArray
* @since 5.0.2
*/
public static void writeObjectAsByteArray(Object obj, DataOutput out)
throws IOException {
Object object = obj;
if (obj instanceof CachedDeserializable) {
// if( ( !(obj instanceof ByteSource) || ((StoredObject) obj).isSerialized())) {
object = ((CachedDeserializable) obj).getSerializedValue();
// }
}
if (DEBUG) {
if (object == null) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "writeObjectAsByteArray null");
} else {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "writeObjectAsByteArray obj.getClass="
+ object.getClass());
}
}
if (object instanceof byte[] || object == null) {
writeByteArray((byte[])object, out);
} else if (out instanceof ObjToByteArraySerializer) {
((ObjToByteArraySerializer)out).writeAsSerializedByteArray(object);
}/*else if (obj instanceof Sendable) {
((Sendable)obj).sendTo(out);
} */
else {
HeapDataOutputStream hdos;
if (object instanceof HeapDataOutputStream) {
hdos = (HeapDataOutputStream)object;
} else {
Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
if (v == null) {
v = Version.CURRENT;
}
hdos = new HeapDataOutputStream(v);
try {
DataSerializer.writeObject(object, hdos);
} catch (IOException e) {
RuntimeException e2 = new IllegalArgumentException(LocalizedStrings.DataSerializer_PROBELM_WHILE_SERIALIZING.toLocalizedString());
e2.initCause(e);
throw e2;
}
}
InternalDataSerializer.writeArrayLength(hdos.size(), out);
hdos.sendTo(out);
}
}
/**
* Reads an array of byte
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeByteArray(byte[], DataOutput)
*/
public static byte[] readByteArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
byte[] array = new byte[length];
in.readFully(array, 0, length);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read byte array of length " + length);
}
return array;
}
}
/**
* Reads an array of byte
s of the given length.
* @param in
* @param length
* @throws IOException
* @see #readByteArray(DataInput)
*/
public static byte[] readByteArray(DataInput in, int length)
throws IOException {
if (length == -1) {
return null;
}
InternalDataSerializer.checkIn(in);
byte[] array = new byte[length];
in.readFully(array, 0, length);
if (DEBUG) {
InternalDataSerializer.logger.info(LocalizedStrings.DEBUG,
"Read byte array of length " + length);
}
return array;
}
/**
* Writes an array of String
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readStringArray
* @see #writeString
*/
public static void writeStringArray(String[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing String array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
writeString(array[i], out);
}
}
}
/**
* Reads an array of String
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeStringArray
*/
public static String[] readStringArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
String[] array = new String[length];
for (int i = 0; i < length; i++) {
array[i] = readString(in);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read String array of length " + length);
}
return array;
}
}
/**
* Writes an array of short
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readShortArray
*/
public static void writeShortArray(short[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing short array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeShort(array[i]);
}
}
}
/**
* Reads an array of short
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeShortArray
*/
public static short[] readShortArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
short[] array = new short[length];
for (int i = 0; i < length; i++) {
array[i] = in.readShort();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read short array of length " + length);
}
return array;
}
}
/**
* Writes an array of char
s to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readCharArray
* @since 5.7
*/
public static void writeCharArray(char[] array, DataOutput out)
throws IOException {
InternalDataSerializer.writeCharArray(array, array != null ? array.length
: -1, out);
}
/**
* Reads an array of char
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeCharArray
* @since 5.7
*/
public static char[] readCharArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
char[] array = new char[length];
for (int i = 0; i < length; i++) {
array[i] = in.readChar();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read char array of length " + length);
}
return array;
}
}
/**
* Writes an array of boolean
s to a
* DataOutput
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readBooleanArray
* @since 5.7
*/
public static void writeBooleanArray(boolean[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing boolean array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeBoolean(array[i]);
}
}
}
/**
* Reads an array of boolean
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeBooleanArray
* @since 5.7
*/
public static boolean[] readBooleanArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
boolean[] array = new boolean[length];
for (int i = 0; i < length; i++) {
array[i] = in.readBoolean();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read boolean array of length " + length);
}
return array;
}
}
/**
* Writes an int
array to a DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readIntArray
*/
public static void writeIntArray(int[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing int array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeInt(array[i]);
}
}
}
/**
* Reads an int
array from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeIntArray
*/
public static int[] readIntArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
int[] array = new int[length];
for (int i = 0; i < length; i++) {
array[i] = in.readInt();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read int array of length " + length);
}
return array;
}
}
/**
* Writes an array of long
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readLongArray
*/
public static void writeLongArray(long[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing long array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeLong(array[i]);
}
}
}
/**
* Reads an array of long
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeLongArray
*/
public static long[] readLongArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
long[] array = new long[length];
for (int i = 0; i < length; i++) {
array[i] = in.readLong();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read long array of length " + length);
}
return array;
}
}
/**
* Writes an array of float
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readFloatArray
*/
public static void writeFloatArray(float[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing float array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeFloat(array[i]);
}
}
}
/**
* Reads an array of float
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeFloatArray
*/
public static float[] readFloatArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
float[] array = new float[length];
for (int i = 0; i < length; i++) {
array[i] = in.readFloat();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read float array of length " + length);
}
return array;
}
}
/**
* Writes an array of double
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readDoubleArray
*/
public static void writeDoubleArray(double[] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
} else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing double array of length " + length);
}
if (length > 0) {
for (int i = 0; i < length; i++) {
out.writeDouble(array[i]);
}
}
}
/**
* Reads an array of double
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeDoubleArray
*/
public static double[] readDoubleArray(DataInput in)
throws IOException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
double[] array = new double[length];
for (int i = 0; i < length; i++) {
array[i] = in.readDouble();
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read double array of length " + length);
}
return array;
}
}
/**
* Writes an array of Object
s to a
* DataOutput
.
* This method will serialize a
* null
array and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readObjectArray
* @see #writeObject(Object, DataOutput)
*/
public static void writeObjectArray(Object[] array, DataOutput out)
throws IOException {
InternalDataSerializer.writeObjectArray(array, out, false);
}
/**
* Reads an array of Object
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*
* @see #writeObjectArray
* @see #readObject
*/
public static Object[] readObjectArray(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
Class> c = null;
byte typeCode = in.readByte();
String typeString = null;
if (typeCode == DSCODE.CLASS) {
typeString = readString(in);
}
GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
boolean lookForPdxInstance = false;
ClassNotFoundException cnfEx = null;
if (typeCode == DSCODE.CLASS
&& cache != null && cache.getPdxReadSerializedByAnyGemFireServices()) {
try {
c = InternalDataSerializer.getCachedClass(typeString);
lookForPdxInstance = true;
} catch (ClassNotFoundException ignore) {
c = Object.class;
cnfEx = ignore;
}
} else {
if (typeCode == DSCODE.CLASS) {
c = InternalDataSerializer.getCachedClass(typeString);
} else {
c = InternalDataSerializer.decodePrimitiveClass(typeCode);
}
}
Object o = null;
if (length > 0) {
o = readObject(in);
if (lookForPdxInstance && o instanceof PdxInstance) {
lookForPdxInstance = false;
c = Object.class;
}
}
Object[] array = (Object[]) Array.newInstance(c, length);
if (length > 0) {
array[0] = o;
}
for (int i = 1; i < length; i++) {
o = readObject(in);
if (lookForPdxInstance && o instanceof PdxInstance) {
// create an Object[] and copy all the entries we already did into it
lookForPdxInstance = false;
c = Object.class;
Object[] newArray = (Object[])Array.newInstance(c, length);
System.arraycopy(array, 0, newArray, 0, i);
array = newArray;
}
array[i] = o;
}
if (lookForPdxInstance && cnfEx != null && length > 0) {
// We have a non-empty array and didn't find any
// PdxInstances in it. So we should have been able
// to load the element type.
// Note that empty arrays in this case will deserialize
// as an Object[] since we can't tell if the element
// type is a pdx one.
throw cnfEx;
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Object array of length " + length);
}
return array;
}
}
/**
* Writes an array of byte[] to a DataOutput.
*
* @throws IOException
* A problem occurs while writing to out.
*
*/
public static void writeArrayOfByteArrays(byte[][] array, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int length;
if (array == null) {
length = -1;
}
else {
length = array.length;
}
InternalDataSerializer.writeArrayLength(length, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing byte[][] of length " + length);
}
if (length >= 0) {
for (int i = 0; i < length; i++) {
writeByteArray(array[i], out);
}
}
}
/**
* Reads an array of byte[]
s from a
* DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
*/
public static byte[][] readArrayOfByteArrays(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int length = InternalDataSerializer.readArrayLength(in);
if (length == -1) {
return null;
} else {
byte[][] array = new byte[length][];
for (int i = 0; i < length; i++) {
array[i] = readByteArray(in);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read byte[][] of length " + length);
}
return array;
}
}
/**
* Writes an ArrayList
to a DataOutput
.
* Note that even though list
may be an instance of a
* subclass of ArrayList
, readArrayList
* will always return an instance of ArrayList
,
* not an instance of the subclass. To preserve the class
* type of list
, {@link #writeObject(Object, DataOutput)} should be used
* for data serialization.
* This method will serialize a
* null
list and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readArrayList
*/
public static void writeArrayList(ArrayList> list, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (list == null) {
size = -1;
} else {
size = list.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing ArrayList with " + size + " elements: " + list);
}
if (size > 0) {
for (Object v : list) {
writeObject(v, out);
}
}
}
/**
* Reads an ArrayList
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the ArrayList
's
* elements cannot be found.
*
* @see #writeArrayList
*/
public static ArrayList readArrayList(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
ArrayList list = new ArrayList(size);
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
list.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read ArrayList with " + size + " elements: " + list);
}
return list;
}
}
/**
* Writes an Vector
to a DataOutput
.
* Note that even though list
may be an instance of a
* subclass of Vector
, readVector
* will always return an instance of Vector
,
* not an instance of the subclass. To preserve the class
* type of list
, {@link #writeObject(Object, DataOutput)} should be used
* for data serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readVector
* @since 5.7
*/
public static void writeVector(Vector> list, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (list == null) {
size = -1;
} else {
size = list.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Vector with " + size + " elements: " + list);
}
if (size > 0) {
for (Object v : list) {
writeObject(v, out);
}
}
}
/**
* Reads an Vector
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the Vector
's
* elements cannot be found.
*
* @see #writeVector
* @since 5.7
*/
public static Vector readVector(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
Vector list = new Vector(size);
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
list.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Vector with " + size + " elements: " + list);
}
return list;
}
}
/**
* Writes an Stack
to a DataOutput
.
* Note that even though list
may be an instance of a
* subclass of Stack
, readStack
* will always return an instance of Stack
,
* not an instance of the subclass. To preserve the class
* type of list
, {@link #writeObject(Object, DataOutput)} should be used
* for data serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readStack
* @since 5.7
*/
public static void writeStack(Stack> list, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (list == null) {
size = -1;
} else {
size = list.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Stack with " + size + " elements: " + list);
}
if (size > 0) {
for (Object v : list) {
writeObject(v, out);
}
}
}
/**
* Reads an Stack
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the Stack
's
* elements cannot be found.
*
* @see #writeStack
* @since 5.7
*/
public static Stack readStack(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
Stack list = new Stack();
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
list.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Stack with " + size + " elements: " + list);
}
return list;
}
}
/**
* Writes an LinkedList
to a DataOutput
.
* Note that even though list
may be an instance of a
* subclass of LinkedList
, readLinkedList
* will always return an instance of LinkedList
,
* not an instance of the subclass. To preserve the class
* type of list
, {@link #writeObject(Object, DataOutput)} should be used
* for data serialization.
* This method will serialize a
* null
list and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readLinkedList
*/
public static void writeLinkedList(LinkedList> list, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (list == null) {
size = -1;
} else {
size = list.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing LinkedList with " + size + " elements: " + list);
}
if (size > 0) {
for (Object e: list) {
writeObject(e, out);
}
}
}
/**
* Reads an LinkedList
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the LinkedList
's
* elements cannot be found.
*
* @see #writeLinkedList
*/
public static LinkedList readLinkedList(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
LinkedList list = new LinkedList();
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
list.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read LinkedList with " + size + " elements: " + list);
}
return list;
}
}
/**
* Writes a HashSet
to a DataOutput
. Note
* that even though set
may be an instance of a
* subclass of HashSet
, readHashSet
will
* always return an instance of HashSet
, not an
* instance of the subclass. To preserve the class type of
* set
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
* This method will serialize a
* null
set and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readHashSet
*/
public static void writeHashSet(HashSet> set, DataOutput out)
throws IOException {
InternalDataSerializer.writeSet(set, out);
}
/**
* Reads a HashSet
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the HashSet
's
* elements cannot be found.
*
* @see #writeHashSet
*/
public static HashSet readHashSet(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
HashSet set = new HashSet(size);
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
set.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read HashSet with " + size + " elements: " + set);
}
return set;
}
}
/**
* Writes a LinkedHashSet
to a DataOutput
. Note
* that even though set
may be an instance of a
* subclass of LinkedHashSet
, readLinkedHashSet
will
* always return an instance of LinkedHashSet
, not an
* instance of the subclass. To preserve the class type of
* set
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readLinkedHashSet
* @since 5.7
*/
public static void writeLinkedHashSet(LinkedHashSet> set, DataOutput out)
throws IOException {
InternalDataSerializer.writeSet(set, out);
}
/**
* Reads a LinkedHashSet
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the LinkedHashSet
's
* elements cannot be found.
*
* @see #writeLinkedHashSet
* @since 5.7
*/
public static LinkedHashSet readLinkedHashSet(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
LinkedHashSet set = new LinkedHashSet(size);
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
set.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read LinkedHashSet with " + size + " elements: " + set);
}
return set;
}
}
/**
* Writes a HashMap
to a DataOutput
. Note
* that even though map
may be an instance of a
* subclass of HashMap
, readHashMap
will
* always return an instance of HashMap
, not an
* instance of the subclass. To preserve the class type of
* map
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
* This method will serialize a
* null
map and not throw a
* NullPointerException
.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readHashMap
*/
public static void writeHashMap(HashMap,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing HashMap with " + size + " elements: " + map);
}
if (size > 0) {
for (Map.Entry,?> entry: map.entrySet()) {
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
public static void writeTHashMap(THashMap map, final DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing THashMap with " + size + " elements: " + map);
}
if (size > 0) {
map.forEachEntry(new InternalDataSerializer.WriteKeyValue(out));
}
}
public static THashMap readTHashMap(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
THashMap map = new THashMap(size);
for (int i = 0; i < size; i++) {
Object key = DataSerializer.readObject(in);
Object value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read THashMap with " + size + " elements: " + map);
}
return map;
}
}
/**
* Reads a HashMap
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the HashMap
's
* elements cannot be found.
*
* @see #writeHashMap
*/
public static HashMap readHashMap(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
HashMap map = new HashMap(size);
for (int i = 0; i < size; i++) {
K key = DataSerializer.readObject(in);
V value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read HashMap with " + size + " elements: " + map);
}
return map;
}
}
/**
* Writes a IdentityHashMap
to a DataOutput
. Note
* that even though map
may be an instance of a
* subclass of IdentityHashMap
, readIdentityHashMap
will
* always return an instance of IdentityHashMap
, not an
* instance of the subclass. To preserve the class type of
* map
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readIdentityHashMap
*/
public static void writeIdentityHashMap(IdentityHashMap,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing IdentityHashMap with " + size + " elements: " + map);
}
if (size > 0) {
for (Map.Entry,?> entry: map.entrySet()){
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
/**
* Reads a IdentityHashMap
from a DataInput
.
* Note that key identity is not preserved unless the keys belong to a class
* whose serialization preserves identity.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the IdentityHashMap
's
* elements cannot be found.
*
* @see #writeIdentityHashMap
*/
public static IdentityHashMap readIdentityHashMap(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
IdentityHashMap map = new IdentityHashMap(size);
for (int i = 0; i < size; i++) {
K key = DataSerializer.readObject(in);
V value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read IdentityHashMap with " + size + " elements: " + map);
}
return map;
}
}
/**
* Writes a ConcurrentHashMap
to a DataOutput
. Note
* that even though map
may be an instance of a
* subclass of ConcurrentHashMap
, readConcurrentHashMap
will
* always return an instance of ConcurrentHashMap
, not an
* instance of the subclass. To preserve the class type of
* map
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
* At this time if {@link #writeObject(Object, DataOutput)} is called with an instance
* of ConcurrentHashMap then it will be serialized with normal java.io Serialization. So
* if you want the keys and values of a ConcurrentHashMap to take advantage of GemFire serialization
* it must be serialized with this method.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readConcurrentHashMap
* @since 6.6
*/
public static void writeConcurrentHashMap(ConcurrentHashMap,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
Collection> entrySnapshot = null;
if (map == null) {
size = -1;
} else {
// take a snapshot to fix bug 44562
entrySnapshot = new ArrayList>(map.entrySet());
size = entrySnapshot.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing ConcurrentHashMap with " + size + " elements: " + entrySnapshot);
}
if (size > 0) {
for (Map.Entry,?> entry: entrySnapshot) {
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
/**
* Reads a ConcurrentHashMap
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the ConcurrentHashMap
's
* elements cannot be found.
*
* @see #writeConcurrentHashMap
* @since 6.6
*/
public static ConcurrentHashMap readConcurrentHashMap(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
ConcurrentHashMap map = new ConcurrentHashMap(size);
for (int i = 0; i < size; i++) {
K key = DataSerializer.readObject(in);
V value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read ConcurrentHashMap with " + size + " elements: " + map);
}
return map;
}
}
/**
* Writes a Hashtable
to a DataOutput
. Note
* that even though map
may be an instance of a
* subclass of Hashtable
, readHashtable
will
* always return an instance of Hashtable
, not an
* instance of the subclass. To preserve the class type of
* map
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readHashtable
* @since 5.7
*/
public static void writeHashtable(Hashtable,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Hashtable with " + size + " elements: " + map);
}
if (size > 0) {
for (Map.Entry,?> entry: map.entrySet()) {
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
/**
* Reads a Hashtable
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the Hashtable
's
* elements cannot be found.
*
* @see #writeHashtable
* @since 5.7
*/
public static Hashtable readHashtable(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
Hashtable map = new Hashtable(size);
for (int i = 0; i < size; i++) {
K key = DataSerializer.readObject(in);
V value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read Hashtable with " + size + " elements: " + map);
}
return map;
}
}
/**
* Writes a TreeMap
to a DataOutput
. Note
* that even though map
may be an instance of a
* subclass of TreeMap
, readTreeMap
will
* always return an instance of TreeMap
, not an
* instance of the subclass. To preserve the class type of
* map
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
* If the map has a comparator then it must also be serializable.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readTreeMap
* @since 5.7
*/
public static void writeTreeMap(TreeMap,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing TreeMap with " + size + " elements: " + map);
}
if (size >= 0) {
writeObject(map.comparator(), out);
for (Map.Entry,?> entry: map.entrySet()) {
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
/**
* Reads a TreeMap
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the TreeMap
's
* elements cannot be found.
*
* @see #writeTreeMap
* @since 5.7
*/
public static TreeMap readTreeMap(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
Comparator super K> c = InternalDataSerializer.>readNonPdxInstanceObject(in);
TreeMap map = new TreeMap(c);
for (int i = 0; i < size; i++) {
K key = DataSerializer.readObject(in);
V value = DataSerializer.readObject(in);
map.put(key, value);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read TreeMap with " + size + " elements: " + map);
}
return map;
}
}
/**
* Writes a TreeSet
to a DataOutput
. Note
* that even though set
may be an instance of a
* subclass of TreeSet
, readTreeSet
will
* always return an instance of TreeSet
, not an
* instance of the subclass. To preserve the class type of
* set
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
* If the set has a comparator then it must also be serializable.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readTreeSet
*/
public static void writeTreeSet(TreeSet> set, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (set == null) {
size = -1;
} else {
size = set.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing TreeSet with " + size + " elements: " + set);
}
if (size >= 0) {
writeObject(set.comparator(), out);
for (Object e: set) {
writeObject(e, out);
}
}
}
/**
* Reads a TreeSet
from a DataInput
.
*
* @throws IOException
* A problem occurs while reading from in
* @throws ClassNotFoundException
* The class of one of the TreeSet
's
* elements cannot be found.
*
* @see #writeTreeSet
*/
public static TreeSet readTreeSet(DataInput in)
throws IOException, ClassNotFoundException {
InternalDataSerializer.checkIn(in);
int size = InternalDataSerializer.readArrayLength(in);
if (size == -1) {
return null;
} else {
Comparator super E> c = InternalDataSerializer.>readNonPdxInstanceObject(in);
TreeSet set = new TreeSet(c);
for (int i = 0; i < size; i++) {
E element = DataSerializer.readObject(in);
set.add(element);
}
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Read TreeSet with " + size + " elements: " + set);
}
return set;
}
}
/**
* Writes a Properties
to a DataOutput
.
* NOTE: The defaults
of the specified props
* are not serialized.
*
Note that even though props
may be an instance of a
* subclass of Properties
, readProperties
will
* always return an instance of Properties
, not an
* instance of the subclass. To preserve the class type of
* props
, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to out
*
* @see #readProperties
*/
public static void writeProperties(Properties props, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
Set> s;
int size;
if (props == null) {
s = null;
size = -1;
} else {
s = props.entrySet();
size = s.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing Properties with " + size + " elements: " + props);
}
if (size > 0) {
for (Map.Entry