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

com.twitter.chill.WrappedArraySerializer.scala Maven / Gradle / Ivy

The newest version!
/*
Copyright 2012 Twitter, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.twitter.chill

import scala.collection.mutable.{ WrappedArray, WrappedArrayBuilder }

import scala.reflect._

class WrappedArraySerializer[T] extends KSerializer[WrappedArray[T]] {

  def write(kser: Kryo, out: Output, obj: WrappedArray[T]) {
    // Write the class-manifest, we don't use writeClass because it
    // uses the registration system, and this class might not be registered
    kser.writeObject(out, obj.elemTag.runtimeClass)
    kser.writeClassAndObject(out, obj.array)
  }

  def read(kser: Kryo, in: Input, cls: Class[WrappedArray[T]]) = {
    // Write the class-manifest, we don't use writeClass because it
    // uses the registration system, and this class might not be registered
    val clazz = kser.readObject(in, classOf[Class[T]])
    val array = kser.readClassAndObject(in).asInstanceOf[Array[T]]
    val bldr = new WrappedArrayBuilder[T](ClassTag[T](clazz))
    bldr.sizeHint(array.size)
    bldr ++= array
    bldr.result()
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy