2016-09-07 7 views
0

어떤 이유로 flatbuffer에서 문자열을 읽을 때 IndexOutOfBoundsExpetion이 표시됩니다.테이블에서 문자열을 읽을 때 indexOutOfBoundExeption

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

, 그리고 내가 내 스키마는 : 내 버퍼를 쓰고 있어요 방법

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; 

입니다 그것으로부터 약간의 데이터를 플랫 버퍼 메시지 (flatbuffer message)로 가져 오기 위해 노력한다. 버퍼를 만들 때 오류가 발생하지 않지만 첫 번째 버퍼에서 버퍼를 읽을 때 IndexOutOfBoundsExeption이 발생합니다. 구문 분석 할 때 String이 null이 아닙니다.

답변

0

버퍼 작성 방법에 문제가없는 것으로 나타납니다.

IndexOutOfBoundsException이 표시되면 일반적으로 버퍼가 만들어지고 읽힐 때 버퍼가 손상되었다는 의미입니다. 버퍼를 읽기 바로 전에 확인할 수 있습니까? 크기와 바이트가 포함 된 크기가 방금 생성 한 때와 동일합니까? 바이너리 파일이나 전송 프로토콜을 사용하고 있습니까?

+0

내 버퍼 쓰기에 실제로 문제가있었습니다. trip array vector offset을 생성하고 추가하는 대신 trip offset을 추가하고있었습니다. 어쨌든 고맙겠 :-) – itayrabin

0

내가 실수로 오프셋 (offset) tripVector로 tripOffset를 추가했습니다 :) 고정!

관련 문제