Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Created May 12, 2014 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gszeliga/b059eb83c4746d0851ba to your computer and use it in GitHub Desktop.
Save gszeliga/b059eb83c4746d0851ba to your computer and use it in GitHub Desktop.
Bencode domain
trait BencodeConstants {
final val DEFAULT_STRING_ENCODING = "ISO-8859-15"
final val DEFAULT_NUMBER_ENCODING = "US-ASCII"
final val NUMBER_BEGIN: Char = 'i'
final val NUMBER_END: Char = 'e'
final val LIST_BEGIN = 'l'
final val LIST_END = 'e'
final val DICT_BEGIN = 'd'
final val DICT_END = 'e'
}
sealed trait BencodeType extends BencodeConstants
case class BString(get: List[Byte]) extends BencodeType {
def create(enc: String = DEFAULT_STRING_ENCODING) = new String(get.toArray, enc)
override def toString = {
//We use a 1-byte encoding since it's compliant with specification
val output = new String(get.take(200).toArray, DEFAULT_STRING_ENCODING)
if (get.length > 200) output + " ..." else output
}
}
case class BInt(get: Int) extends BencodeType {
override def toString = get.toString
}
case class BList(get: List[BencodeType]) extends BencodeType
case class BDict(get: Map[BString, BencodeType]) extends BencodeType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment