从表中读取字符串时的indexOutOfBoundExeption

时间:2016-09-07 20:02:58

标签: java lucene flatbuffers

由于某种原因,我在从flatbuffer读取字符串时得到一个IndexOutOfBoundsExpetion。 我的架构:

// :)
internal bool DoThisThing_ShouldBeALambda() {
    string toggleButtonName = "hi!";
    return DoThisThing(toggleButtonName);
}

internal bool DoThisThing(string input) {...} // Takes a while.

private void toggleButton_Click(object sender, RoutedEventArgs e)
{
    Task<bool> task = new Task<bool>(DoThisThing_ShouldBeALambda); 

    // ... do other things for a while

    // Task takes a while, don't want to block UI thread, use Task.
    task.Start();

    // ... keep on going and let Task finish. Maybe check the result, maybe not.        
}

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

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);
}

我有一个lucene数据库,我试图从中获取一些数据到flatbuffer消息。 在创建缓冲区时我没有错误,但是从第一个缓冲区读取缓冲区时我得到一个IndexOutOfBoundsExeption。 我检查过,解析时String不为null。

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

修复它:)

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

答案 2 :(得分:0)

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