0

I am getting an IndexOutOfBoundsExpetion when reading a string from a flatbuffer for some reason. my schema:

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;

this is how I'm writing my buffer:

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

and these are my models:

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

I have a lucene database, and I am trying to get some data from it to a flatbuffer message. when creating the buffer I get no errors, but I get an IndexOutOfBoundsExeption when reading the buffer, from the first one. I checked, and the String is not null when parsing.

itayrabin
  • 398
  • 1
  • 2
  • 12

3 Answers3

0

There doesn't appear anything wrong with how you you create the buffer.

If you get an IndexOutOfBoundsException, that typically means the buffer got corrupted between when it got created and read. Can you check just before you read the buffer, that the size and the bytes it contains are the same compared to when you just created it? Are you sure you are using a binary file or transfer protocol?

Aardappel
  • 5,559
  • 1
  • 19
  • 22
  • There actually was something wrong with my buffer writing. I was adding the trip offset instead of creating a trip array vector offset and adding that. Thanks anyway though :-) – itayrabin Sep 08 '16 at 04:43
0

fixed it :)

I was adding the tripOffset as the tripVector offset by mistake!

itayrabin
  • 398
  • 1
  • 2
  • 12
0

For what it's worth, I got a similar error when my ByteBuffer was too small. The write seemingly went fine, but, in reality, it got truncated. The read threw an IOOBE. When I increased the size of the buffer, everything worked fine.

colin
  • 129
  • 2