@Override public long tell() throws IOException { return is.getPos(); }
@Override public long tell() throws IOException { return is.getPos(); }
/** * Creates a {@link StreamEvent} that will be used as a template for all events consumable from this reader. */ private StreamEvent createEventTemplate(Map<String, String> properties) throws IOException { long timestamp = -1L; // See if all events in the file are of the same timestamp String uniTimestamp = properties.get(StreamDataFileConstants.Property.Key.UNI_TIMESTAMP); if (StreamDataFileConstants.Property.Value.CLOSE_TIMESTAMP.equals(uniTimestamp)) { // Seek to the end - 8 of the stream to read the close timestamp long pos = eventInput.getPos(); eventInput.seek(eventInput.size() - 8); timestamp = Math.abs(readTimestamp()); eventInput.seek(pos); } else if (uniTimestamp != null) { timestamp = Long.parseLong(uniTimestamp); } // Grab the set of default headers for all events ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); String prefix = StreamDataFileConstants.Property.Key.EVENT_HEADER_PREFIX; for (Map.Entry<String, String> entry : properties.entrySet()) { if (entry.getKey().startsWith(prefix)) { headers.put(entry.getKey().substring(prefix.length()), entry.getValue()); } } return new StreamEvent(headers.build(), ByteBuffers.EMPTY_BUFFER, timestamp); }
/** * Fills the internal buffer by reading from the given input stream. * * @param input input stream to read from * @param size number of bytes to read * @throws IOException if failed to read from the stream * @throws EOFException if failed to read the given number of bytes from the input */ void fillBuffer(SeekableInputStream input, int size) throws IOException { buffer.clear(); buffer = ensureCapacity(buffer, size); try { basePosition = input.getPos(); int bytesRead = 0; while (bytesRead != size) { int len = input.read(buffer.array(), bytesRead, size - bytesRead); if (len < 0) { throw new EOFException("Expected to read " + size + ", but only " + bytesRead + " was read"); } bytesRead += len; } buffer.limit(size); bufferInput.reset(buffer); } catch (IOException e) { // Make the buffer has nothing to read buffer.position(buffer.limit()); basePosition = -1L; throw e; } }
private void readHeader() throws IOException { // Read the header of the event file // First 2 bytes should be 'E' '1' byte[] magic = new byte[StreamDataFileConstants.MAGIC_HEADER_SIZE]; ByteStreams.readFully(eventInput, magic); int fileVersion = decodeFileVersion(magic); // Read the properties map. Map<String, String> properties = StreamUtils.decodeMap(new BinaryDecoder(eventInput)); verifySchema(properties); // Create event template if (fileVersion >= 2) { eventTemplate = createEventTemplate(properties); } else { eventTemplate = new StreamEvent(ImmutableMap.<String, String>of(), ByteBuffers.EMPTY_BUFFER, -1L); } position = eventInput.getPos(); }