0

出于某种原因从平面缓冲区读取字符串时,我得到了 IndexOutOfBoundsExpetion。我的架构:

namespace com.busalarmclock.flatbuffers;

table Message {
    routes:[Route];
    stops:[Stop];
    trips:[Trip];
}

table Route {
    route_id:string;
    route_name:string;
    route_description:string;
    trips:[Trip];
}

table Trip {
    trip_id:string;
    op_days:int;
    stops:[TripStop];
}

table Stop {
    stop_id:int;
    stop_name:string;
    stop_lon:double;
    stop_lat:double;
}

table TripStop {
    stop:Stop;
    arrival_time:long;
    departure_time:long;
    dropoff_type:short;
}

root_type Message;

这就是我写缓冲区的方式:

public static byte[] createStopMessage(TopDocs hits, IndexSearcher indexSearcher) throws IOException {
    FlatBufferBuilder builder = new FlatBufferBuilder(1);
    int[] stopData = new int[hits.totalHits];

    for (int i = 0; i < hits.totalHits; i++)
        stopData[i] = createStopObject(indexSearcher.doc(hits.scoreDocs[i].doc), builder);

    int stopsOffset = Message.createStopsVector(builder, stopData);
    Message.startMessage(builder);
    Message.addStops(builder, stopsOffset);
    int root = Message.endMessage(builder);
    builder.finish(root);

    return builder.sizedByteArray();
}

 public static byte[] createTripStopsMessage(TripModel trip, IndexSearcher indexSearcher) {
    FlatBufferBuilder builder = new FlatBufferBuilder(1);
    int[] tripStopData = new int[trip.tripStopModels.length];

    for (int i = 0; i < trip.tripStopModels.length; i++)
        tripStopData[i] = createTripStopObject(trip.tripStopModels[i], builder);

    System.out.printf("tripId:%s", trip.tripId);
    int tripIdOffset = builder.createString(trip.tripId);
    int tripStopsOffset = Trip.createStopsVector(builder, tripStopData);

    Trip.startTrip(builder);
    Trip.addTripId(builder, tripIdOffset);
    Trip.addStops(builder, tripStopsOffset);
    int tripOffset = Trip.endTrip(builder);

    Message.startMessage(builder);
    Message.addTrips(builder, tripOffset);
    int messageOffset = Message.endMessage(builder);
    builder.finish(messageOffset);

    return builder.sizedByteArray();
}

public static int createTripStopObject(TripStopModel tripStopModel, FlatBufferBuilder builder) {
    int stopOffset = createStopObject(tripStopModel.stop, builder);
    return TripStop.createTripStop(builder, stopOffset, tripStopModel.arrivalTime,
            tripStopModel.departureTime, tripStopModel.dropoffType);
}

这些是我的模型:

public class TripModel {
public String tripId;
public int opDays;
public TripStopModel[] tripStopModels;

public TripModel() {
}

public TripModel(String tripId) {
    this.tripId = tripId;
}

public TripModel(String tripId, TripStopModel[] tripStationHits) {
    this.tripStopModels = tripStationHits;
    this.tripId = tripId;
}

public TripModel(String tripId, int opDays, TripStopModel[] tripStationHits) {
    this.tripId = tripId;
    this.opDays = opDays;
    this.tripStopModels = tripStationHits;
}

import org.apache.lucene.document.Document;

/**
 * Created by User on 09/07/2016.
 */
public class TripStopModel {
    public long arrivalTime;
    public long departureTime;
    public short dropoffType;
    public Document stop;

    public TripStopModel() {
    }

    public TripStopModel(long arrivalTime, long departureTime, short dropoffType, Document stop) {
        this.arrivalTime = arrivalTime;
        this.departureTime = departureTime;
        this.dropoffType = dropoffType;
        this.stop = stop;
    }
}

我有一个 lucene 数据库,我正在尝试从中获取一些数据到 flatbuffer 消息中。创建缓冲区时,我没有收到任何错误,但是从第一个缓冲区读取缓冲区时,我得到了 IndexOutOfBoundsExeption。我检查了,解析时字符串不为空。

4

3 回答 3

0

对于它的价值,当我的 ByteBuffer 太小时,我遇到了类似的错误。写入看起来很顺利,但实际上,它被截断了。阅读引发了IOOBE。当我增加缓冲区的大小时,一切正常。

于 2018-12-09T01:02:55.843 回答
0

解决它 :)

我错误地将tripOffset 添加为tripVector 偏移量!

于 2016-09-08T15:03:02.820 回答
0

您创建缓冲区的方式似乎没有任何问题。

如果你得到一个IndexOutOfBoundsException,这通常意味着缓冲区在它被创建和读取之间被破坏了。您能否在读取缓冲区之前检查它包含的大小和字节与您刚创建它时是否相同?您确定您使用的是二进制文件或传输协议吗?

于 2016-09-07T20:33:30.703 回答