예제 #1
0
func (r *rawV4EventReader) getHeaderBuffer() *bufio2.LookAheadBuffer {
	if r.headerBuffer == nil { // New event
		r.headerBuffer = bufio2.NewLookAheadBufferUsing(
			r.src,
			r.rawHeaderBuffer)
	}
	return r.headerBuffer
}
예제 #2
0
func (r *rawV4EventReader) NextEvent() (Event, error) {
	if r.isClosed {
		return nil, errors.New("Event reader is closed")
	}

	if r.nextEvent == nil { // new event
		r.nextEvent = &RawV4Event{
			sourceName:     r.srcName,
			sourcePosition: r.logPosition,
		}
	}

	if r.nextEvent.data == nil { // still parsing the header
		headerBytes, err := r.getHeaderBuffer().PeekAll()
		if err != nil {
			return nil, err
		}

		_, err = readLittleEndian(headerBytes, &r.nextEvent.header)
		if err != nil {
			return nil, err
		}

		bodySize := int(r.nextEvent.EventLength()) - sizeOfBasicV4EventHeader
		if bodySize < 0 { // should never happen
			return nil, errors.New("Invalid event size")
		}

		r.nextEvent.data = make(
			[]byte,
			r.nextEvent.EventLength(),
			r.nextEvent.EventLength())

		copied := copy(r.nextEvent.data, headerBytes)
		if copied != sizeOfBasicV4EventHeader { // should never happen
			panic("Failed to copy header")
		}

		r.bodyBuffer = bufio2.NewLookAheadBufferUsing(
			r.src,
			r.nextEvent.data[sizeOfBasicV4EventHeader:])
	}

	_, err := r.bodyBuffer.PeekAll()
	if err != nil {
		return nil, err
	}

	// consume the constructed event and clean the look ahead buffers
	event := r.nextEvent
	r.nextEvent = nil
	r.headerBuffer = nil
	r.bodyBuffer = nil

	r.logPosition += int64(event.EventLength())

	return event, nil
}