All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dorkbox.serializers.RegexSerializer.kt Maven / Gradle / Ivy

The newest version!
package dorkbox.serializers

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.util.regex.Pattern

/**
 * Kryo [Serializer] for regex [Pattern]s.
 *
 * @author [Martin Grotzke](mailto:[email protected])
 * @author serverperformance
 */
class RegexSerializer : Serializer() {
    init {
        isImmutable = true
    }

    override fun write(kryo: Kryo, output: Output, pattern: Pattern) {
        output.writeString(pattern.pattern())
        output.writeInt(pattern.flags(), true)
    }

    override fun read(kryo: Kryo, input: Input, patternClass: Class): Pattern {
        val regex = input.readString()
        val flags = input.readInt(true)
        return Pattern.compile(regex, flags)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy