1
1
package com .sksamuel .avro4s .encoders
2
2
3
- import com .sksamuel .avro4s .{ Avro4sConfigurationException , Avro4sEncodingException , Encoder , FieldMapper }
4
- import org . apache . avro . Conversions . UUIDConversion
5
- import org .apache .avro .{ Conversions , LogicalTypes , Schema }
3
+ import com .sksamuel .avro4s .avroutils . ByteBufferHelper
4
+ import com . sksamuel . avro4s .{ Avro4sConfigurationException , Avro4sEncodingException , Encoder }
5
+ import org .apache .avro .Schema
6
6
import org .apache .avro .generic .GenericData
7
7
import org .apache .avro .util .Utf8
8
8
@@ -44,6 +44,9 @@ object ByteStringEncoder extends Encoder[String] :
44
44
*/
45
45
object FixedStringEncoder extends Encoder [String ] :
46
46
override def encode (schema : Schema ): String => Any = string =>
47
- if (string.getBytes.length > schema.getFixedSize)
48
- throw new Avro4sEncodingException (s " Cannot write string with ${string.getBytes.length} bytes to fixed type of size ${schema.getFixedSize}" )
49
- GenericData .get.createFixed(null , ByteBuffer .allocate(schema.getFixedSize).put(string.getBytes).array, schema).asInstanceOf [GenericData .Fixed ]
47
+ val bytes = string.getBytes
48
+ if (bytes.length > schema.getFixedSize)
49
+ throw new Avro4sEncodingException (s " Cannot write string with ${bytes.length} bytes to fixed type of size ${schema.getFixedSize}" )
50
+ GenericData .get.createFixed(null ,
51
+ ByteBufferHelper .asArray(ByteBuffer .allocate(schema.getFixedSize).put(bytes)),
52
+ schema).asInstanceOf [GenericData .Fixed ]
0 commit comments