{"id":1934,"date":"2013-09-07T23:41:55","date_gmt":"2013-09-07T23:41:55","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=1934"},"modified":"2013-09-07T23:41:55","modified_gmt":"2013-09-07T23:41:55","slug":"base64-encoding-and-decoding-example-in-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/base64-encoding-and-decoding-example-in-scala\/","title":{"rendered":"Base64 Encoding and Decoding Example in Scala"},"content":{"rendered":"<p>This fairly simple example makes use of the core Java APIs to read files, demonstrating how to construct a base-64 encoded version, and then generating the original file again.<\/p>\n<pre lang=\"scala\">package com.garysieling {\n  import java.io.{File, FileInputStream, FileOutputStream}\n  import sun.misc.{BASE64Encoder, BASE64Decoder}\n\n  object Base64 extends App {\n    val filename = \"\"\"C:\\IdeaProjects\\base64\\tests.pdf\"\"\"\n    Console.println(\"Testing \" + filename)\n\n    val file = new File(filename)\n    val in = new FileInputStream(file)\n    val bytes = new Array[Byte](file.length.toInt)\n    in.read(bytes)\n    in.close()\n\n    val encodedFile = new File(filename + \".base64\")\n    val encoded = \n       new BASE64Encoder()\n         .encode(bytes)\n         .replace(\"\\n\", \"\")\n         .replace(\"\\r\", \"\")\n    val encodedStream = new FileOutputStream(encodedFile)\n    encodedStream.write(encoded.getBytes)\n    encodedStream.close()\n\n    val decodedFile = new File(filename + \".decoded\")\n    val decoded = new BASE64Decoder().decodeBuffer(encoded)\n    val decodedStream = new FileOutputStream(decodedFile)\n    decodedStream.write(decoded)\n    decodedStream.close()\n  }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This fairly simple example makes use of the core Java APIs to read files, demonstrating how to construct a base-64 encoded version, and then generating the original file again. package com.garysieling { import java.io.{File, FileInputStream, FileOutputStream} import sun.misc.{BASE64Encoder, BASE64Decoder} object Base64 extends App { val filename = &#8220;&#8221;&#8221;C:\\IdeaProjects\\base64\\tests.pdf&#8221;&#8221;&#8221; Console.println(&#8220;Testing &#8221; + filename) val file = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/base64-encoding-and-decoding-example-in-scala\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Base64 Encoding and Decoding Example in Scala&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[300,480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1934"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1934"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1934\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}