Skip to content
Permalink
Browse files
converted WriteObjectState to an enum
  • Loading branch information
joe-chacko committed Apr 17, 2015
1 parent 92289be commit 95a9c8894a3b3b50ff0cee500ad6ba4f7d301122
Showing 1 changed file with 83 additions and 197 deletions.
@@ -35,186 +35,105 @@ abstract class ObjectWriter extends java.io.ObjectOutputStream {

private PutFieldImpl _putFields;

private WriteObjectState state = NOT_IN_WRITE_OBJECT;
private WriteObjectState state = WriteObjectState.NOT_IN_WRITE_OBJECT;

private byte streamFormatVersion = 1;

static class WriteObjectState {
void beforeWriteObject(ObjectWriter writer) throws IOException {
}

void afterWriteObject(ObjectWriter writer) throws IOException {
}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
}

void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
}

void beforeWriteData(ObjectWriter writer) throws IOException {
}
}

static WriteObjectState NOT_IN_WRITE_OBJECT = new WriteObjectState() {
void beforeWriteObject(ObjectWriter writer) throws IOException {
writer.state = IN_WRITE_OBJECT;
}

void afterWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}

void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}
};

static WriteObjectState IN_WRITE_OBJECT = new WriteObjectState() {
void beforeWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("already in writeObject");
}

void afterWriteObject(ObjectWriter writer) throws IOException {

writer.state = NOT_IN_WRITE_OBJECT;

// there is no custom (nor default) state written
writer.writeBoolean(false);

// If we're in stream format verison 2, we must
// put the "null" marker to say that there isn't
// any optional data
if (writer.getStreamFormatVersion() == 2) {
writer._nullValue();
private enum WriteObjectState {
NOT_IN_WRITE_OBJECT {
void beforeWriteObject(ObjectWriter writer) throws IOException {
writer.state = IN_WRITE_OBJECT;
}
},
IN_WRITE_OBJECT {
void afterWriteObject(ObjectWriter writer) throws IOException {
writer.state = NOT_IN_WRITE_OBJECT;

// there is no custom (nor default) state written
writer.writeBoolean(false);

// If we're in stream format version 2, we must
// put the "null" marker to say that there isn't
// any optional data
if (writer.getStreamFormatVersion() == 2) {
writer._nullValue();
}
}

}
void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
writer.state = IN_DEFAULT_WRITE_OBJECT;
writer.writeBoolean(true);
}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
writer.state = IN_DEFAULT_WRITE_OBJECT;
writer.writeBoolean(true);
}
void beforeWriteData(ObjectWriter writer) throws IOException {
writer.state = WROTE_CUSTOM_DATA;

void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}
// writeDefaultObject was not invoked
writer.writeBoolean(false);

/*
* (non-Javadoc)
*
* @see org.apache.yoko.rmi.impl.ObjectWriter.WriteObjectState#writeData(org.apache.yoko.rmi.impl.ObjectWriter)
*/
void beforeWriteData(ObjectWriter writer) throws IOException {
writer.state = WROTE_CUSTOM_DATA;
if (writer.getStreamFormatVersion() == 2) {
writer._startValue(writer._desc.getCustomRepositoryID());
}
}
},
IN_DEFAULT_WRITE_OBJECT {
void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
writer.state = WROTE_DEFAULT_DATA;
}
},
WROTE_DEFAULT_DATA {
void afterWriteObject(ObjectWriter writer) throws IOException {
// the boolean identifying that default data was written has already
// been emitted in IN_WRITE_OBJECT.beforeWriteDefaultObject
writer.state = NOT_IN_WRITE_OBJECT;

// write a null-marker to identify that there is no custom
// state being marshalled...
if (writer.getStreamFormatVersion() == 2) {
writer._nullValue();
}
}

// writeDefaultObject was not invoked
writer.writeBoolean(false);
void beforeWriteData(ObjectWriter writer) throws IOException {
// the boolean identifying that default data was written has already
// been emitted in IN_WRITE_OBJECT.beforeWriteDefaultObject
writer.state = WROTE_CUSTOM_DATA;

if (writer.getStreamFormatVersion() == 2) {
writer._startValue(writer._desc.getCustomRepositoryID());
if (writer.getStreamFormatVersion() == 2) {
writer._startValue(writer._desc.getCustomRepositoryID());
}
}
},
WROTE_CUSTOM_DATA {
void afterWriteObject(ObjectWriter writer) throws IOException {
if (writer.getStreamFormatVersion() == 2) {
writer._endValue();
}

writer.state = NOT_IN_WRITE_OBJECT;
}

}
};

static WriteObjectState IN_DEFAULT_WRITE_OBJECT = new WriteObjectState() {
;

void beforeWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
throw new IllegalStateException();
}

void afterWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
throw new IllegalStateException();
}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
throw new IllegalStateException();
}

void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
writer.state = WROTE_DEFAULT_DATA;
throw new IllegalStateException();
}

void beforeWriteData(ObjectWriter writer) throws IOException {
// do nothing
}
};

static WriteObjectState WROTE_DEFAULT_DATA = new WriteObjectState() {

void afterWriteObject(ObjectWriter writer) throws IOException {

// the boolean identifying that default data was written has already
// been emitted in IN_WRITE_OBJECT.beforeWriteDefaultObject

writer.state = NOT_IN_WRITE_OBJECT;

// write a null-marker to identify that there is no custom
// state being marshalled...
if (writer.getStreamFormatVersion() == 2) {
writer._nullValue();
}

}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}

void afterDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}

void beforeWriteData(ObjectWriter writer) throws IOException {

// the boolean identifying that default data was written has already
// been emitted in IN_WRITE_OBJECT.beforeWriteDefaultObject

writer.state = WROTE_CUSTOM_DATA;

if (writer.getStreamFormatVersion() == 2) {
writer._startValue(writer._desc.getCustomRepositoryID());
}

}
};

static WriteObjectState WROTE_CUSTOM_DATA = new WriteObjectState() {
/*
* (non-Javadoc)
*
* @see org.apache.yoko.rmi.impl.ObjectWriter.WriteObjectState#afterWriteObject(org.apache.yoko.rmi.impl.ObjectWriter)
*/
void afterWriteObject(ObjectWriter writer) throws IOException {
if (writer.getStreamFormatVersion() == 2) {
writer._endValue();
}

writer.state = NOT_IN_WRITE_OBJECT;
}

void beforeDefaultWriteObject(ObjectWriter writer) throws IOException {
throw new IllegalStateException("");
}

void afterDefaultWriteObject(ObjectWriter writer) {
throw new IllegalStateException("");
}

/*
* (non-Javadoc)
*
* @see org.apache.yoko.rmi.impl.ObjectWriter.WriteObjectState#writeData(org.apache.yoko.rmi.impl.ObjectWriter)
*/
void beforeWriteData(ObjectWriter writer) throws IOException {
// do nothing
}
};
}

protected void beforeWriteData() throws IOException {
state.beforeWriteData(this);
@@ -225,9 +144,6 @@ protected void beforeWriteData() throws IOException {
streamFormatVersion = CmsfThreadLocalStack.peek();
}

/**
* @return
*/
protected byte getStreamFormatVersion() {
return streamFormatVersion;
}
@@ -305,90 +221,63 @@ static class PutFieldImpl extends PutField {
this.slice = slice;
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, boolean)
*/
@Override
public void put(String name, boolean val) {
valueMap.put(name, new Boolean(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, byte)
*/
@Override
public void put(String name, byte val) {
valueMap.put(name, new Byte(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, char)
*/
@Override
public void put(String name, char val) {
valueMap.put(name, new Character(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, double)
*/
@Override
public void put(String name, double val) {
valueMap.put(name, new Double(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, float)
*/
@Override
public void put(String name, float val) {
valueMap.put(name, new Float(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, int)
*/
@Override
public void put(String name, int val) {
valueMap.put(name, new Integer(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, long)
*/
@Override
public void put(String name, long val) {
valueMap.put(name, new Long(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, Object)
*/
@Override
public void put(String name, Object val) {
valueMap.put(name, val);
}

/**
* @see java.io.ObjectOutputStream.PutField#put(String, short)
*/
@Override
public void put(String name, short val) {
valueMap.put(name, new Short(val));
}

/**
* @see java.io.ObjectOutputStream.PutField#write(ObjectOutput)
* @deprecated
*/
@Override
public void write(ObjectOutput out) throws IOException {
slice.writeFields((ObjectWriter) out, valueMap);
}

}

/**
* @param descriptor
* @param val
* @param _write_object_method
*/
void invokeWriteObject(ValueDescriptor descriptor, Serializable val,
Method _write_object_method) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, IOException {
ValueDescriptor desc = _desc;
WriteObjectState old_state = state;
state = NOT_IN_WRITE_OBJECT;
state = WriteObjectState.NOT_IN_WRITE_OBJECT;
try {
setCurrentValueDescriptor(descriptor);
writeByte(getStreamFormatVersion());
@@ -407,9 +296,6 @@ void invokeWriteObject(ValueDescriptor descriptor, Serializable val,

protected abstract void _nullValue() throws IOException;

/**
* @param externalizable
*/
void invokeWriteExternal(Externalizable externalizable) throws IOException {
writeByte(getStreamFormatVersion());
externalizable.writeExternal(this);

0 comments on commit 95a9c88

Please sign in to comment.