-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
321 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
centurion-avro/src/main/kotlin/com/sksamuel/centurion/avro/io/DataReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.sksamuel.centurion.avro.io | ||
|
||
import org.apache.avro.Schema | ||
import org.apache.avro.file.CodecFactory | ||
import org.apache.avro.file.DataFileReader | ||
import org.apache.avro.file.SeekableByteArrayInput | ||
import org.apache.avro.file.SeekableInput | ||
import org.apache.avro.generic.GenericDatumReader | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.avro.io.DatumReader | ||
import org.apache.avro.io.DecoderFactory | ||
|
||
/** | ||
* Creates a [DataReaderFactory] for a given schema which can then be used to create [DataReader]s. | ||
* | ||
* All readers created from this factory share a thread safe [DatumReader] for efficiency. | ||
* | ||
* Pass in a pre-created [DecoderFactory] if you wish to configure buffer size. | ||
*/ | ||
class DataReaderFactory( | ||
reader: Schema, | ||
writer: Schema, | ||
private val factory: DecoderFactory, | ||
private val codecFactory: CodecFactory, | ||
) { | ||
|
||
private val datum = GenericDatumReader<GenericRecord>(reader, writer) | ||
|
||
/** | ||
* Creates a [DataReader] that reads from the given [ByteArray]. | ||
*/ | ||
fun reader(bytes: ByteArray): DataReader { | ||
return DataReader(SeekableByteArrayInput(bytes), datum) | ||
} | ||
} | ||
|
||
class DataReader(private val input: SeekableInput, datum: DatumReader<GenericRecord>) : AutoCloseable { | ||
|
||
private val reader = DataFileReader.openReader(input, datum) | ||
|
||
fun hasNext() = reader.hasNext() | ||
fun read(): GenericRecord = reader.next() | ||
fun iterator() = reader.iterator() | ||
|
||
override fun close() { | ||
reader.close() | ||
input.close() | ||
} | ||
} |
Oops, something went wrong.