下面列出了java.io.ObjectOutput#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see java.io.Externalizable#writeExternal(ObjectOutput)
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
// The operation
out.writeInt( operation.getValue() );
// The EntryAttribute if not null
if ( attribute != null )
{
out.writeBoolean( true );
attribute.writeExternal( out );
}
else
{
out.writeBoolean( false );
}
out.flush();
}
/**
* Write to server.
* WARNING: To maintain backwards compatibility never change the order or insert new writes, always append to
* {@link org.apache.openejb.client.EJBRequest.Body#writeExternal(java.io.ObjectOutput)}
*
* @param out ObjectOutput
* @throws IOException
*/
@Override
public void writeExternal(final ObjectOutput out) throws IOException {
out.writeByte(requestMethod.getCode());
if (deploymentCode > 0) {
out.writeObject(null);
} else {
out.writeObject(deploymentId);
}
out.writeShort(deploymentCode);
out.writeObject(clientIdentity);
out.writeInt(serverHash);
out.flush();
body.setMetaData(metaData);
body.writeExternal(out);
}
/**
* {@inheritDoc}
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
out.writeUTF( oid );
out.writeBoolean( criticality );
if ( hasValue() )
{
out.writeBoolean( true );
out.writeInt( value.length );
if ( value.length > 0 )
{
out.write( value );
}
}
else
{
out.writeBoolean( false );
}
out.flush();
}
/**
* {@inheritDoc}
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
if ( upName == null )
{
String message = I18n.err( I18n.ERR_13624_CANNOT_SERIALIZE_NULL_DN );
LOG.error( message );
throw new IOException( message );
}
// Write the UPName
out.writeUTF( upName );
// Write the RDNs.
// First the number of RDNs
out.writeInt( size() );
// Loop on the RDNs
for ( Rdn rdn : rdns )
{
rdn.writeExternal( out );
}
out.flush();
}
private byte[] writeObject(Object o) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.flush();
return bos.toByteArray();
}
}
private static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutput out = new DroolsObjectOutputStream(bytes);
out.writeObject(obj);
out.flush();
out.close();
return bytes.toByteArray();
}
private static byte[] marshal(Object obj) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bytes);
out.writeObject(obj);
out.flush();
out.close();
return bytes.toByteArray();
}
public void testReadWriteExternal() throws IOException, ClassNotFoundException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
final ObjectOutput oos = new ObjectOutputStream(baos);
oos.writeObject(byteArrayKey);
oos.flush();
assertEquals(byteArrayKey, new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject());
}
byte[] serialize(UserAgentAnalyzerTester uaa) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(uaa);
out.flush();
return bos.toByteArray();
}
}
/** Serialization/deserialization. */
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
ClientConfiguration target = new ClientConfiguration()
.setAddresses("127.0.0.1:10800", "127.0.0.1:10801")
.setTimeout(123)
.setBinaryConfiguration(new BinaryConfiguration()
.setClassNames(Collections.singleton("Person"))
)
.setSslMode(SslMode.REQUIRED)
.setSslClientCertificateKeyStorePath("client.jks")
.setSslClientCertificateKeyStoreType("JKS")
.setSslClientCertificateKeyStorePassword("123456")
.setSslTrustCertificateKeyStorePath("trust.jks")
.setSslTrustCertificateKeyStoreType("JKS")
.setSslTrustCertificateKeyStorePassword("123456")
.setSslKeyAlgorithm("SunX509");
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(outBytes);
out.writeObject(target);
out.flush();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(outBytes.toByteArray()));
Object desTarget = in.readObject();
assertTrue(Comparers.equal(target, desTarget));
}
/**
* This is the place where we serialize entries, and all theirs
* elements.
* <br>
* The structure used to store the entry is the following :
* <ul>
* <li>
* <b>[Dn]</b> : If it's null, stores an empty Dn
* </li>
* <li>
* <b>[attributes number]</b> : the number of attributes.
* </li>
* <li>
* <b>[attribute]*</b> : each attribute, if we have some
* </li>
* </ul>
*
* {@inheritDoc}
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
// First, the Dn
if ( dn == null )
{
// Write an empty Dn
Dn.EMPTY_DN.writeExternal( out );
}
else
{
// Write the Dn
dn.writeExternal( out );
}
// Then the attributes.
// Store the attributes' nulber first
out.writeInt( attributes.size() );
// Iterate through the keys.
for ( Attribute attribute : attributes.values() )
{
// Store the attribute
attribute.writeExternal( out );
}
out.flush();
}
/**
* This is the place where we serialize entries, and all theirs
* elements.
* <br/>
* The structure used to store the entry is the following :
* <ul>
* <li>
* <b>[Dn]</b> : If it's null, stores an empty Dn
* </li>
* <li>
* <b>[attributes number]</b> : the number of attributes.
* </li>
* <li>
* <b>[attribute]*</b> : each attribute, if we have some
* </li>
* </ul>
*
* {@inheritDoc}
*/
public void writeExternal( ObjectOutput out ) throws IOException
{
// First, the Dn
if ( dn == null )
{
// Write an empty Dn
Dn.EMPTY_DN.writeExternal( out );
}
else
{
// Write the Dn
dn.writeExternal( out );
}
// Then the attributes.
// Store the attributes' nulber first
out.writeInt( attributes.size() );
// Iterate through the keys.
for ( Attribute attribute : attributes.values() )
{
// Store the attribute
attribute.writeExternal( out );
}
out.flush();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(_bitSize);
out.writeInt(_mask);
out.writeObject(_bits);
out.flush();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(totalFreq);
trie.save(out);
out.flush();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(_bitSize);
out.writeInt(_mask);
out.writeObject(_bits);
out.flush();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(_bitSize);
out.writeInt(_mask);
out.writeObject(_bits);
out.flush();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(_bitSize);
out.writeInt(_mask);
out.writeObject(_bits);
out.flush();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(_bitSize);
out.writeInt(_mask);
out.writeObject(_bits);
out.flush();
}
/**
* {@inheritDoc}
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
// Write a boolean for the HR flag
out.writeBoolean( isHR );
if ( isHR )
{
// Write the value if any
out.writeBoolean( upValue != null );
if ( upValue != null )
{
// Write the value
out.writeInt( bytes.length );
if ( bytes.length > 0 )
{
out.write( bytes );
}
}
// Write the prepared value if any
out.writeBoolean( normValue != null );
if ( normValue != null )
{
// Write the value
out.writeUTF( normValue );
}
}
else
{
// Just write the bytes if not null
out.writeBoolean( bytes != null );
if ( bytes != null )
{
out.writeInt( bytes.length );
if ( bytes.length > 0 )
{
out.write( bytes );
}
}
}
// and flush the data
out.flush();
}
/** Serialization/deserialization. */
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
ClientCacheConfiguration target = new ClientCacheConfiguration().setName("Person")
.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)
.setBackups(3)
.setCacheMode(CacheMode.PARTITIONED)
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC)
.setEagerTtl(false)
.setGroupName("FunctionalTest")
.setDefaultLockTimeout(12345)
.setPartitionLossPolicy(PartitionLossPolicy.READ_WRITE_ALL)
.setReadFromBackup(true)
.setRebalanceBatchSize(67890)
.setRebalanceBatchesPrefetchCount(102938)
.setRebalanceDelay(54321)
.setRebalanceMode(CacheRebalanceMode.SYNC)
.setRebalanceOrder(2)
.setRebalanceThrottle(564738)
.setRebalanceTimeout(142536)
.setKeyConfiguration(new CacheKeyConfiguration("Employee", "orgId"))
.setQueryEntities(new QueryEntity(int.class.getName(), "Employee")
.setTableName("EMPLOYEE")
.setFields(
Stream.of(
new SimpleEntry<>("id", Integer.class.getName()),
new SimpleEntry<>("orgId", Integer.class.getName())
).collect(Collectors.toMap(
SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new
))
)
.setKeyFields(Collections.singleton("id"))
.setNotNullFields(Collections.singleton("id"))
.setDefaultFieldValues(Collections.singletonMap("id", 0))
.setIndexes(Collections.singletonList(new QueryIndex("id", true, "IDX_EMPLOYEE_ID")))
.setAliases(Stream.of("id", "orgId").collect(Collectors.toMap(f -> f, String::toUpperCase)))
);
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(outBytes);
out.writeObject(target);
out.flush();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(outBytes.toByteArray()));
Object desTarget = in.readObject();
assertTrue(Comparers.equal(target, desTarget));
}