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

ujson.ByteArrayParser.scala Maven / Gradle / Ivy

There is a newer version: 3.0.0-M2
Show newest version
package ujson

import scala.annotation.{switch, tailrec}
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets

import upickle.core.{ObjArrVisitor, Visitor}
/**
  * Basic ByteBuffer parser.
  *
  * This assumes that the provided ByteBuffer is ready to be read. The
  * user is responsible for any necessary flipping/resetting of the
  * ByteBuffer before parsing.
  *
  * The parser makes absolute calls to the ByteBuffer, which will not
  * update its own mutable position fields.
  */
final class ByteArrayParser[J](src: Array[Byte]) extends ByteParser[J]{

  val srcLength = src.length
  protected[this] final def close() = {}

  // Never grow the buffer since it's a directly using the original
  override def growBuffer(until: Int): Unit = ()

  def readDataIntoBuffer(buffer: Array[Byte], bufferOffset: Int) = {
    if(buffer == null) (src, srcLength == 0, srcLength)
    else (src, true, -1)
  }
}

object ByteArrayParser extends Transformer[Array[Byte]]{
  def transform[T](j: Array[Byte], f: Visitor[_, T]) = new ByteArrayParser(j).parse(f)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy