Created
May 12, 2014 22:01
-
-
Save gszeliga/b059eb83c4746d0851ba to your computer and use it in GitHub Desktop.
Bencode domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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