use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.
the class JGroupsMessenger method createJGMessage.
/**
* This is the constructor to use to create a JGroups message holding a GemFire
* DistributionMessage. It sets the appropriate flags in the Message and properly serializes the
* DistributionMessage for the recipient's product version
*
* @param gfmsg the DistributionMessage
* @param src the sender address
* @param version the version of the recipient
* @return the new message
*/
Message createJGMessage(DistributionMessage gfmsg, JGAddress src, short version) {
if (gfmsg instanceof DirectReplyMessage) {
((DirectReplyMessage) gfmsg).registerProcessor();
}
Message msg = new Message();
msg.setDest(null);
msg.setSrc(src);
setMessageFlags(gfmsg, msg);
try {
long start = services.getStatistics().startMsgSerialization();
HeapDataOutputStream out_stream = new HeapDataOutputStream(Version.fromOrdinalOrCurrent(version));
Version.CURRENT.writeOrdinal(out_stream, true);
if (encrypt != null) {
out_stream.writeBoolean(true);
writeEncryptedMessage(gfmsg, version, out_stream);
} else {
out_stream.writeBoolean(false);
serializeMessage(gfmsg, out_stream);
}
msg.setBuffer(out_stream.toByteArray());
services.getStatistics().endMsgSerialization(start);
} catch (IOException | GemFireIOException ex) {
logger.warn("Error serializing message", ex);
if (ex instanceof GemFireIOException) {
throw (GemFireIOException) ex;
} else {
GemFireIOException ioe = new GemFireIOException("Error serializing message");
ioe.initCause(ex);
throw ioe;
}
} catch (Exception ex) {
logger.warn("Error serializing message", ex);
GemFireIOException ioe = new GemFireIOException("Error serializing message");
ioe.initCause(ex.getCause());
throw ioe;
}
return msg;
}
use of org.apache.geode.internal.HeapDataOutputStream 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;
}
use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.
the class DiskInitFile method writeIFRecord.
private void writeIFRecord(byte b, DiskRegionView dr, String s) {
assert lock.isHeldByCurrentThread();
try {
int hdosSize = 1 + DR_ID_MAX_BYTES + estimateByteSize(s) + 1;
if (hdosSize < 32) {
hdosSize = 32;
}
HeapDataOutputStream hdos = new HeapDataOutputStream(hdosSize, Version.CURRENT);
hdos.write(b);
writeDiskRegionID(hdos, dr.getId());
hdos.writeUTF(s);
hdos.write(END_OF_RECORD_ID);
writeIFRecord(hdos, true);
} catch (IOException ex) {
DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
if (!this.compactInProgress) {
this.parent.handleDiskAccessException(dae);
}
throw dae;
}
}
use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.
the class DiskInitFile method writeClearRecord.
/**
* Write a clear with an RVV record.
*/
private void writeClearRecord(DiskRegionView dr, RegionVersionVector rvv) {
try {
HeapDataOutputStream hdos = new HeapDataOutputStream(32, Version.CURRENT);
hdos.write(IFREC_CLEAR_REGION_WITH_RVV_ID);
writeDiskRegionID(hdos, dr.getId());
// We only need the memberToVersionMap for clear purposes
Map<DiskStoreID, RegionVersionHolder> memberToVersion = rvv.getMemberToVersion();
hdos.writeInt(memberToVersion.size());
for (Map.Entry<DiskStoreID, RegionVersionHolder> entry : memberToVersion.entrySet()) {
InternalDataSerializer.invokeToData(entry.getKey(), hdos);
synchronized (entry.getValue()) {
InternalDataSerializer.invokeToData(entry.getValue(), hdos);
}
}
hdos.write(END_OF_RECORD_ID);
// don't do stats for these small records
writeIFRecord(hdos, false);
} catch (IOException ex) {
DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
if (!this.compactInProgress) {
this.parent.handleDiskAccessException(dae);
}
throw dae;
}
}
use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.
the class DiskInitFile method writeRevokedMember.
private void writeRevokedMember(PersistentMemberPattern revoked) {
try {
HeapDataOutputStream hdos = new HeapDataOutputStream(32, Version.CURRENT);
hdos.write(IFREC_REVOKE_DISK_STORE_ID);
InternalDataSerializer.invokeToData(revoked, hdos);
hdos.write(END_OF_RECORD_ID);
// don't do stats for these small records
writeIFRecord(hdos, false);
} catch (IOException ex) {
DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
if (!this.compactInProgress) {
this.parent.handleDiskAccessException(dae);
}
throw dae;
}
}
Aggregations