private static PortablePosition createPositionForSingleCellPrimitiveArrayAccess( PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { // assumes position.getIndex() >= 0 BufferObjectDataInput in = ctx.getIn(); in.position(getStreamPositionOfTheField(ctx)); // read the array length and ignore it, it has been already validated in.readInt(); int streamPosition; if (ctx.getCurrentFieldType() == FieldType.UTF || ctx.getCurrentFieldType() == FieldType.UTF_ARRAY) { // UTF arrays actually need iterating over all elements to read the length of each element // it's impossible to dead-reckon about a cell's position int currentIndex = 0; while (index > currentIndex) { int indexElementLen = in.readInt(); indexElementLen = indexElementLen < 0 ? 0 : indexElementLen; in.position(in.position() + indexElementLen); currentIndex++; } streamPosition = in.position(); } else { // in primitive non-utf arrays we can dead-reckon about a cell's position streamPosition = in.position() + index * ctx.getCurrentFieldType().getSingleType().getTypeSize(); } return PortablePositionFactory.createSinglePrimitivePosition(ctx.getCurrentFieldDefinition(), streamPosition, index, path.isLastToken()); }
private static PortablePosition createPositionForSingleCellPrimitiveArrayAccess( PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { // assumes position.getIndex() >= 0 BufferObjectDataInput in = ctx.getIn(); in.position(getStreamPositionOfTheField(ctx)); // read the array length and ignore it, it has been already validated in.readInt(); int streamPosition; if (ctx.getCurrentFieldType() == FieldType.UTF || ctx.getCurrentFieldType() == FieldType.UTF_ARRAY) { // UTF arrays actually need iterating over all elements to read the length of each element // it's impossible to dead-reckon about a cell's position int currentIndex = 0; while (index > currentIndex) { int indexElementLen = in.readInt(); indexElementLen = indexElementLen < 0 ? 0 : indexElementLen; in.position(in.position() + indexElementLen); currentIndex++; } streamPosition = in.position(); } else { // in primitive non-utf arrays we can dead-reckon about a cell's position streamPosition = in.position() + index * ctx.getCurrentFieldType().getSingleType().getTypeSize(); } return PortablePositionFactory.createSinglePrimitivePosition(ctx.getCurrentFieldDefinition(), streamPosition, index, path.isLastToken()); }