Search in sources :

Example 1 with NullDataOutputStream

use of org.apache.geode.internal.NullDataOutputStream in project geode by apache.

the class CachedDeserializableFactory method calcMemSize.

/**
   * If not calcSerializedSize then return -1 if we can't figure out the mem size.
   */
public static int calcMemSize(Object o, ObjectSizer os, boolean addOverhead, boolean calcSerializedSize) {
    int result;
    if (o instanceof byte[]) {
        // does not need to be wrapped so overhead never added
        result = getByteSize((byte[]) o);
        addOverhead = false;
    } else if (o == null) {
        // does not need to be wrapped so overhead never added
        result = 0;
        addOverhead = false;
    } else if (o instanceof String) {
        result = // for the length of the char[]
        (((String) o).length() * 2) + 4 + // for String obj and Char[] obj
        (Sizeable.PER_OBJECT_OVERHEAD * 2) + // for obj ref to char[] on String; note should be 8 on 64-bit vm
        4 + // for offset int field on String
        4 + // for count int field on String
        4 + // for hash int field on String
        4;
    } else if (o instanceof byte[][]) {
        result = getArrayOfBytesSize((byte[][]) o, true);
        addOverhead = false;
    } else if (o instanceof CachedDeserializable) {
        // overhead never added
        result = ((CachedDeserializable) o).getSizeInBytes();
        addOverhead = false;
    } else if (o instanceof Sizeable) {
        result = ((Sizeable) o).getSizeInBytes();
    } else if (os != null) {
        result = os.sizeof(o);
    } else if (calcSerializedSize) {
        result = Sizeable.PER_OBJECT_OVERHEAD + 4;
        NullDataOutputStream dos = new NullDataOutputStream();
        try {
            DataSerializer.writeObject(o, dos);
            result += dos.size();
        } catch (IOException ex) {
            RuntimeException ex2 = new IllegalArgumentException(LocalizedStrings.CachedDeserializableFactory_COULD_NOT_CALCULATE_SIZE_OF_OBJECT.toLocalizedString());
            ex2.initCause(ex);
            throw ex2;
        }
    } else {
        // return -1 to signal the caller that we did not compute the size
        result = -1;
        addOverhead = false;
    }
    if (addOverhead) {
        result += overhead();
    }
    // RuntimeException("STACK"));
    return result;
}
Also used : Sizeable(org.apache.geode.internal.cache.lru.Sizeable) NullDataOutputStream(org.apache.geode.internal.NullDataOutputStream) IOException(java.io.IOException)

Example 2 with NullDataOutputStream

use of org.apache.geode.internal.NullDataOutputStream in project geode by apache.

the class CachedDeserializableFactory method calcSerializedSize.

/**
   * Return an estimate of the number of bytes this object will consume when serialized. This is the
   * number of bytes that will be written on the wire including the 4 bytes needed to encode the
   * length.
   */
public static int calcSerializedSize(Object o) {
    int result;
    if (o instanceof byte[]) {
        result = getByteSize((byte[]) o) - Sizeable.PER_OBJECT_OVERHEAD;
    } else if (o instanceof byte[][]) {
        result = getArrayOfBytesSize((byte[][]) o, false);
    } else if (o instanceof CachedDeserializable) {
        result = ((CachedDeserializable) o).getSizeInBytes() + 4 - overhead();
    } else if (o instanceof Sizeable) {
        result = ((Sizeable) o).getSizeInBytes() + 4;
    } else if (o instanceof HeapDataOutputStream) {
        result = ((HeapDataOutputStream) o).size() + 4;
    } else {
        result = 4;
        NullDataOutputStream dos = new NullDataOutputStream();
        try {
            DataSerializer.writeObject(o, dos);
            result += dos.size();
        } catch (IOException ex) {
            RuntimeException ex2 = new IllegalArgumentException(LocalizedStrings.CachedDeserializableFactory_COULD_NOT_CALCULATE_SIZE_OF_OBJECT.toLocalizedString());
            ex2.initCause(ex);
            throw ex2;
        }
    }
    // RuntimeException("STACK"));
    return result;
}
Also used : Sizeable(org.apache.geode.internal.cache.lru.Sizeable) NullDataOutputStream(org.apache.geode.internal.NullDataOutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 NullDataOutputStream (org.apache.geode.internal.NullDataOutputStream)2 Sizeable (org.apache.geode.internal.cache.lru.Sizeable)2 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1