提交 78018191 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Move exception handler to top parseEWKB() method

上级 573e605d
...@@ -269,7 +269,11 @@ public final class EWKBUtils { ...@@ -269,7 +269,11 @@ public final class EWKBUtils {
* output target * output target
*/ */
public static void parseEWKB(byte[] ewkb, Target target) { public static void parseEWKB(byte[] ewkb, Target target) {
parseEWKB(new EWKBSource(ewkb), target, 0, 0); try {
parseEWKB(new EWKBSource(ewkb), target, 0, 0);
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException();
}
} }
/** /**
...@@ -286,120 +290,115 @@ public final class EWKBUtils { ...@@ -286,120 +290,115 @@ public final class EWKBUtils {
* root geometry (will be determined from the EWKB instead) * root geometry (will be determined from the EWKB instead)
*/ */
private static void parseEWKB(EWKBSource source, Target target, int parentType, int parentSrid) { private static void parseEWKB(EWKBSource source, Target target, int parentType, int parentSrid) {
try { // Read byte order of a next geometry
// Read byte order of a next geometry switch (source.readByte()) {
switch (source.readByte()) { case 0:
case 0: source.bigEndian = true;
source.bigEndian = true; break;
break; case 1:
case 1: source.bigEndian = false;
source.bigEndian = false; break;
break; default:
default: throw new IllegalArgumentException();
}
// Type contains type of a geometry and additional flags
int type = source.readInt();
// PostGIS extensions
boolean useZ = (type & EWKB_Z) != 0;
boolean useM = (type & EWKB_M) != 0;
int srid = (type & EWKB_SRID) != 0 ? source.readInt() : 0;
// Preserve parent SRID unconditionally
if (parentType != 0) {
srid = parentSrid;
}
// OGC 06-103r4
type &= 0xffff;
switch (type / 1_000) {
case DIMENSION_SYSTEM_XYZ:
useZ = true;
break;
case DIMENSION_SYSTEM_XYZM:
useZ = true;
//$FALL-THROUGH$
case DIMENSION_SYSTEM_XYM:
useM = true;
}
type %= 1_000;
switch (type) {
case POINT:
if (parentType != 0 && parentType != MULTI_POINT && parentType != GEOMETRY_COLLECTION) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
// Type contains type of a geometry and additional flags target.startPoint(srid);
int type = source.readInt(); addCoordinate(source, target, useZ, useM, 0, 1);
// PostGIS extensions break;
boolean useZ = (type & EWKB_Z) != 0; case LINE_STRING: {
boolean useM = (type & EWKB_M) != 0; if (parentType != 0 && parentType != MULTI_LINE_STRING && parentType != GEOMETRY_COLLECTION) {
int srid = (type & EWKB_SRID) != 0 ? source.readInt() : 0; throw new IllegalArgumentException();
// Preserve parent SRID unconditionally
if (parentType != 0) {
srid = parentSrid;
} }
// OGC 06-103r4 int numPoints = source.readInt();
type &= 0xffff; if (numPoints < 0 || numPoints == 1) {
switch (type / 1_000) { throw new IllegalArgumentException();
case DIMENSION_SYSTEM_XYZ:
useZ = true;
break;
case DIMENSION_SYSTEM_XYZM:
useZ = true;
//$FALL-THROUGH$
case DIMENSION_SYSTEM_XYM:
useM = true;
} }
type %= 1_000; target.startLineString(srid, numPoints);
switch (type) { for (int i = 0; i < numPoints; i++) {
case POINT: addCoordinate(source, target, useZ, useM, i, numPoints);
if (parentType != 0 && parentType != MULTI_POINT && parentType != GEOMETRY_COLLECTION) {
throw new IllegalArgumentException();
}
target.startPoint(srid);
addCoordinate(source, target, useZ, useM, 0, 1);
break;
case LINE_STRING: {
if (parentType != 0 && parentType != MULTI_LINE_STRING && parentType != GEOMETRY_COLLECTION) {
throw new IllegalArgumentException();
}
int numPoints = source.readInt();
if (numPoints < 0 || numPoints == 1) {
throw new IllegalArgumentException();
}
target.startLineString(srid, numPoints);
for (int i = 0; i < numPoints; i++) {
addCoordinate(source, target, useZ, useM, i, numPoints);
}
break;
} }
case POLYGON: { break;
if (parentType != 0 && parentType != MULTI_POLYGON && parentType != GEOMETRY_COLLECTION) { }
throw new IllegalArgumentException(); case POLYGON: {
} if (parentType != 0 && parentType != MULTI_POLYGON && parentType != GEOMETRY_COLLECTION) {
int numInner = source.readInt() - 1; throw new IllegalArgumentException();
if (numInner < 0) { }
throw new IllegalArgumentException(); int numInner = source.readInt() - 1;
} if (numInner < 0) {
int size = source.readInt(); throw new IllegalArgumentException();
// Size may be 0 (EMPTY) or 4+ }
if (size < 0 || size >= 1 && size <= 3) { int size = source.readInt();
throw new IllegalArgumentException(); // Size may be 0 (EMPTY) or 4+
} if (size < 0 || size >= 1 && size <= 3) {
if (size == 0 && numInner > 0) { throw new IllegalArgumentException();
throw new IllegalArgumentException(); }
} if (size == 0 && numInner > 0) {
target.startPolygon(srid, numInner, size); throw new IllegalArgumentException();
if (size > 0) { }
addRing(source, target, useZ, useM, size); target.startPolygon(srid, numInner, size);
for (int i = 0; i < numInner; i++) { if (size > 0) {
size = source.readInt(); addRing(source, target, useZ, useM, size);
// Size may be 0 (EMPTY) or 4+ for (int i = 0; i < numInner; i++) {
if (size < 0 || size >= 1 && size <= 3) { size = source.readInt();
throw new IllegalArgumentException(); // Size may be 0 (EMPTY) or 4+
} if (size < 0 || size >= 1 && size <= 3) {
target.startPolygonInner(size); throw new IllegalArgumentException();
addRing(source, target, useZ, useM, size);
} }
target.endNonEmptyPolygon(); target.startPolygonInner(size);
addRing(source, target, useZ, useM, size);
} }
break; target.endNonEmptyPolygon();
} }
case MULTI_POINT: break;
case MULTI_LINE_STRING: }
case MULTI_POLYGON: case MULTI_POINT:
case GEOMETRY_COLLECTION: { case MULTI_LINE_STRING:
if (parentType != 0 && parentType != GEOMETRY_COLLECTION) { case MULTI_POLYGON:
throw new IllegalArgumentException(); case GEOMETRY_COLLECTION: {
} if (parentType != 0 && parentType != GEOMETRY_COLLECTION) {
int numItems = source.readInt(); throw new IllegalArgumentException();
if (numItems < 0) {
throw new IllegalArgumentException();
}
target.startCollection(type, srid, numItems);
for (int i = 0; i < numItems; i++) {
Target innerTarget = target.startCollectionItem(i, numItems);
parseEWKB(source, innerTarget, type, srid);
target.endCollectionItem(innerTarget, i, numItems);
}
target.endCollection(type);
break;
} }
default: int numItems = source.readInt();
if (numItems < 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} catch (ArrayIndexOutOfBoundsException e) { target.startCollection(type, srid, numItems);
e.printStackTrace(); for (int i = 0; i < numItems; i++) {
Target innerTarget = target.startCollectionItem(i, numItems);
parseEWKB(source, innerTarget, type, srid);
target.endCollectionItem(innerTarget, i, numItems);
}
target.endCollection(type);
break;
}
default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论