下面列出了怎么用org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl.EObjectInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Internal low level factory method.
* @noreference This method is not intended to be referenced by clients.
* @since 2.4
*/
public static QualifiedName createFromStream(EObjectInputStream eObjectInputStream) throws IOException{
int segmentCount = eObjectInputStream.readCompressedInt();
if (segmentCount == 0) {
return QualifiedName.EMPTY;
}
// lowercase QN serialize a 'null' value at index 0 and
String firstSegment = eObjectInputStream.readSegmentedString();
boolean lowerCase = false;
if (firstSegment == null) {
lowerCase = true;
// first was null, read another string which is the actual first segment
firstSegment = eObjectInputStream.readSegmentedString();
if(firstSegment == null){
throw new IllegalStateException("Read unexpected first segment from object stream");
}
}
String[] segments = readSegmentArray(eObjectInputStream, segmentCount, firstSegment);
if (lowerCase) {
return new QualifiedNameLowerCase(segments);
} else {
return new QualifiedName(segments);
}
}
@Test public void testDeserializeAsLowerCase() throws IOException {
QualifiedName upperCase = QualifiedName.create("A", "B");
QualifiedName lowerCase = upperCase.toLowerCase();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(bos, Collections.emptyMap());
upperCase.writeToStream(out);
lowerCase.writeToStream(out);
out.flush();
EObjectInputStream in = new BinaryResourceImpl.EObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), Collections.emptyMap());
QualifiedName readUpperCase = QualifiedName.createFromStream(in);
QualifiedName readLowerCase = QualifiedName.createFromStream(in);
assertEquals(QualifiedName.class.getName(), readUpperCase.getClass().getName());
assertEquals(QualifiedName.class.getName() + "$QualifiedNameLowerCase", readLowerCase.getClass().getName());
assertEquals(upperCase, readUpperCase);
assertEquals(lowerCase, readLowerCase);
}
private static String[] readSegmentArray(EObjectInputStream from, int count, String first) throws IOException {
String[] segments = new String[count];
segments[0] = intern(first);
for (int i = 1; i < count; i++) {
String segment = from.readSegmentedString();
if(segment == null){
throw new IllegalStateException("Read unexpected segment (#" + i + ") from object stream");
}
segments[i] = intern(segment);
}
return segments;
}
@Override
public QualifiedName read(EObjectInputStream eObjectInputStream) throws IOException {
return QualifiedName.createFromStream(eObjectInputStream);
}
@Override
public URI read(EObjectInputStream eObjectInputStream) throws IOException {
return eObjectInputStream.readURI();
}