当前位置: 首页>>代码示例>>Scala>>正文


Scala UnsupportedEncodingException类代码示例

本文整理汇总了Scala中java.io.UnsupportedEncodingException的典型用法代码示例。如果您正苦于以下问题:Scala UnsupportedEncodingException类的具体用法?Scala UnsupportedEncodingException怎么用?Scala UnsupportedEncodingException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了UnsupportedEncodingException类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: JsonToByteArrayUtil

//设置package包名称以及导入依赖的类
package it.agilelab.bigdata.wasp.core.utils

import java.io.UnsupportedEncodingException


object JsonToByteArrayUtil {

  def jsonToByteArray(data: String): Array[Byte] = {
    val opData: Option[String] = Option(data)
    try {
      opData.map(_.getBytes("UTF-8")).orNull
    } catch {
      case e: UnsupportedEncodingException =>
        throw new UnsupportedEncodingException("Error when serializing JsValue (toString) to Array[Byte] due to unsupported encoding UTF8")
    }
  }

  def byteArrayToJson(binary: Array[Byte]): String = {
    try {
      new String(binary, "UTF-8")
    } catch {
      case e: UnsupportedEncodingException =>
        throw new UnsupportedEncodingException("Error when deserializing Array[Byte] to (string) JsValue due to unsupported encoding UTF-8");
    }
  }

} 
开发者ID:agile-lab-dev,项目名称:wasp,代码行数:28,代码来源:JsonToByteArrayUtil.scala

示例2: DetectEncoding

//设置package包名称以及导入依赖的类
package ch.becompany.akka.io

import java.io.{InputStream, UnsupportedEncodingException}

import org.mozilla.universalchardet.UniversalDetector

import scala.util.{Failure, Success, Try}

object DetectEncoding {

  def apply(in: InputStream): String =
    try {
      val detector = new UniversalDetector(null)
      var nread: Int = 0
      var buf = new Array[Byte](4096)
      while ({ nread = in.read(buf); nread } > 0 && !detector.isDone()) {
        detector.handleData(buf, 0, nread)
      }
      detector.dataEnd()

      detector.getDetectedCharset() match {
        case null => throw new UnsupportedEncodingException()
        case s => s
      }
    } finally {
      in.close()
    }

} 
开发者ID:becompany,项目名称:akka-file-io,代码行数:30,代码来源:DetectEncoding.scala

示例3: Bytes

//设置package包名称以及导入依赖的类
package com.avsystem.scex.util

import java.io.UnsupportedEncodingException
import java.nio.charset.StandardCharsets

import com.avsystem.commons.jiop.JavaInterop._
import com.avsystem.scex.presentation.annotation.Documentation
import org.apache.commons.codec.binary.{Base64, Hex}

import scala.collection.mutable
import scala.util.hashing.MurmurHash3

final class Bytes(val bytes: Array[Byte]) extends Comparable[Bytes] {
  override def hashCode(): Int = MurmurHash3.bytesHash(bytes)
  override def equals(other: Any): Boolean = other match {
    case b: Bytes => java.util.Arrays.equals(bytes, b.bytes)
    case _ => false
  }
  override def toString: String = s"Bytes($escaped)"
  override def compareTo(o: Bytes): Int = {
    def loop(i: Int): Int =
      if (i == bytes.length && i == o.bytes.length) 0
      else if (i == bytes.length) -1
      else if (i == o.bytes.length) 1
      else {
        val b1 = bytes(i)
        val b2 = o.bytes(i)
        if (b1 == b2) loop(i + 1) else b1 - b2
      }
    loop(0)
  }

  @Documentation("Encodes this sequence of bytes as string with non-ASCII bytes and backslash escaped, e.g. 'hsg\\x7c\\x0dfoo\\\\bar'")
  def escaped: String = EscapedBytes.render(bytes)

  @Documentation("Encodes this sequence of bytes as hexadecimal string")
  def hex: String = Hex.encodeHexString(bytes)

  @Documentation("Encodes this sequence of bytes as BASE64 string")
  def base64: String = Base64.encodeBase64String(bytes)

  @Documentation("Decodes this sequence of bytes as UTF-8 string")
  def decodeUTF8: String = new String(bytes, StandardCharsets.UTF_8)

  @Documentation("Decodes this sequence of bytes as string using given charset")
  def decode(charset: String): String =
    try new String(bytes, charset) catch {
      case e: UnsupportedEncodingException => throw new IllegalArgumentException(e)
    }

  def asList: JList[Byte] = new mutable.WrappedArray.ofByte(bytes).asJava
} 
开发者ID:AVSystem,项目名称:scex,代码行数:53,代码来源:Bytes.scala

示例4: WebKeywordsQuery

//设置package包名称以及导入依赖的类
package ir.web

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

import ir.query.KeywordsQuery
import text.{StringNone, StringOption}
import text.normalizer.NormalizedString


class WebKeywordsQuery(override val keywords: Seq[String]) extends KeywordsQuery(keywords) with WebQuery {
  override val query: NormalizedString = try {
    NormalizedString(StringOption(URLEncoder.encode(generate, StandardCharsets.UTF_8.name)))
  } catch {
    case e: UnsupportedEncodingException =>
      e.printStackTrace()
      NormalizedString(StringNone)
  }

  protected val delimiter: Char = ' '

  private def generate: String = {
    val builder: StringBuilder = new StringBuilder(keywords.size * 2 - 1)
    keywords foreach {
      query: String =>
        builder.append(delimiter).
                append(query)
    }
    builder.deleteCharAt(0).result
  }
} 
开发者ID:ktr-skmt,项目名称:FelisCatusZero,代码行数:33,代码来源:WebKeywordsQuery.scala

示例5: WebKeywordQuery

//设置package包名称以及导入依赖的类
package ir.web

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

import ir.query.KeywordQuery
import text.StringNone
import text.normalizer.NormalizedString


class WebKeywordQuery(override val keyword: NormalizedString) extends KeywordQuery(keyword) with WebQuery {
  override val query: NormalizedString = try {
    NormalizedString(
      keyword.toStringOption map {
        k: String =>
          URLEncoder.encode(
            k.toString,
            StandardCharsets.UTF_8.name)
    })
  } catch {
    case e: UnsupportedEncodingException =>
      e.printStackTrace()
      NormalizedString(StringNone)
  }
} 
开发者ID:ktr-skmt,项目名称:FelisCatusZero,代码行数:27,代码来源:WebKeywordQuery.scala

示例6: WebKeywordsQuery

//设置package包名称以及导入依赖的类
package us.feliscat.ir.web

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

import us.feliscat.ir.query.KeywordsQuery
import us.feliscat.text.{StringNone, StringOption}


class WebKeywordsQuery(override val keywords: Seq[String]) extends KeywordsQuery(keywords) with WebQuery {
  override val query: StringOption = try {
    StringOption(URLEncoder.encode(generate, StandardCharsets.UTF_8.name))
  } catch {
    case e: UnsupportedEncodingException =>
      e.printStackTrace()
      StringNone
  }

  protected val delimiter: Char = ' '

  private def generate: String = {
    val builder = new StringBuilder(keywords.size * 2 - 1)
    keywords foreach {
      query: String =>
        builder.append(delimiter).
                append(query)
    }
    builder.deleteCharAt(0).result
  }
} 
开发者ID:ktr-skmt,项目名称:FelisCatusZero-multilingual,代码行数:32,代码来源:WebKeywordsQuery.scala

示例7: WebKeywordQuery

//设置package包名称以及导入依赖的类
package us.feliscat.ir.web

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

import us.feliscat.ir.query.KeywordQuery
import us.feliscat.text.{StringNone, StringOption}


class WebKeywordQuery(override val keyword: StringOption) extends KeywordQuery(keyword) with WebQuery {
  override val query: StringOption = try {
    keyword map {
      k: String =>
        URLEncoder.encode(
          k.toString,
          StandardCharsets.UTF_8.name)
    }
  } catch {
    case e: UnsupportedEncodingException =>
      e.printStackTrace()
      StringNone
  }
} 
开发者ID:ktr-skmt,项目名称:FelisCatusZero-multilingual,代码行数:25,代码来源:WebKeywordQuery.scala


注:本文中的java.io.UnsupportedEncodingException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。