alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (UTF8Codec.scala)

This example Scala source code file (UTF8Codec.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, character, codec, illegalargumentexception, int, int, string, string, uni_replacement_bytes, uni_replacement_char, use, use, utf-8

The Scala UTF8Codec.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */


package scala.io

/**
 *  @author  Martin Odersky
 *  @version 1.0, 04/10/2004
 */
object UTF8Codec
{
  final val UNI_REPLACEMENT_CHAR: Int = 0x0000FFFD
  final val UNI_REPLACEMENT_BYTES = Array[Byte](-17, -65, -67)
  
  // Note, from http://unicode.org/faq/utf_bom.html#utf8-5
  //
  // A different issue arises if an unpaired surrogate is encountered when converting
  // ill-formed UTF-16 data. By represented such an unpaired surrogate on its own as a
  // 3-byte sequence, the resulting UTF-8 data stream would become ill-formed.
  // While it faithfully reflects the nature of the input, Unicode conformance
  // requires that encoding form conversion always results in valid data stream.
  // Therefore a converter must treat this as an error.
  //
  // Some useful locations:
  //    http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt

  @deprecated("""Use new String(Array(ch), 0, 1).getBytes("UTF-8") instead""", "2.8.0")
  def encode(ch: Int): Array[Byte] =
    if ((Character getType ch) == Character.SURROGATE.toInt) UNI_REPLACEMENT_BYTES
    else try new String(Array(ch), 0, 1) getBytes "UTF-8" catch {
      case _: IllegalArgumentException  => UNI_REPLACEMENT_BYTES
    }

  @deprecated("Use Codec.toUTF8 instead", "2.8.0")
  def encode(src: Array[Char], from: Int, dst: Array[Byte], to: Int, len: Int): Int = {
    val bytes = Codec toUTF8 src.slice(from, from + len)
    Array.copy(bytes, 0, dst, to, bytes.length)
    bytes.length
  }

  @deprecated("Use Codec.toUTF8 instead", "2.8.0")
  def encode(s: String, dst: Array[Byte], to: Int): Int =
    encode(s.toArray, 0, dst, to, s.length)

  @deprecated("Use Codec.toUTF8 instead", "2.8.0")
  def encode(s: String): Array[Byte] = Codec toUTF8 s

  @deprecated("Use Codec.fromUTF8 instead", "2.8.0")
  def decode(src: Array[Byte], from: Int, dst: Array[Char], to: Int, len: Int): Int = {
    val chars = Codec fromUTF8 src.slice(from, from + len)
    Array.copy(chars, 0, dst, to, chars.length)
    chars.length
  }

  @deprecated("Use Codec.fromUTF8 instead", "2.8.0")
  def decode(src: Array[Byte], from: Int, len: Int): String =
    Codec fromUTF8 src.slice(from, from + len) mkString
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala UTF8Codec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.