<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 3theye</title>
    <description>The latest articles on DEV Community by 3theye (@3theye).</description>
    <link>https://dev.to/3theye</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F347878%2F14fd4eb2-5a0d-44b4-9389-04768a1584a9.jpeg</url>
      <title>DEV Community: 3theye</title>
      <link>https://dev.to/3theye</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/3theye"/>
    <language>en</language>
    <item>
      <title>[tips]Download File in Android with OkHttp</title>
      <dc:creator>3theye</dc:creator>
      <pubDate>Mon, 17 May 2021 11:47:02 +0000</pubDate>
      <link>https://dev.to/3theye/download-file-in-android-with-okhttp-2o63</link>
      <guid>https://dev.to/3theye/download-file-in-android-with-okhttp-2o63</guid>
      <description>&lt;h4&gt;
  
  
  Reading the file
&lt;/h4&gt;

&lt;p&gt;ตอนที่เราดาวน์โหลดมันข้อมูลที่เราได้จะมาในรูปแบบ stream of data เราจึงต้องอ่านทีละส่วนเพื่อประกอบเป็นไฟล์ลงในเครื่อง&lt;/p&gt;

&lt;p&gt;ซึ่งใน Kotlin เราสามารถทำได้อย่างง่ายดายด้วย code ไม่กี่บรรทัด ตัวอย่างด้านล่าง&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body.byteStream().apply {
    file.outputStream().use { fileOut -&amp;gt;
        copyTo(fileOut, BUFFER_LENGTH_BYTES)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;อย่างไรก็ตามการใช้ copyTo มีข้อเสียอย่างหนึ่งคือเราไม่สามารถรู้ progress ในการทำงานได้&lt;/p&gt;

</description>
      <category>kotlin</category>
    </item>
    <item>
      <title>[tips]ทำให้การอ่านไฟล์เป็นเรื่องง่ายด้วยคำสั่ง USE ใน Kotlin</title>
      <dc:creator>3theye</dc:creator>
      <pubDate>Mon, 17 May 2021 11:31:26 +0000</pubDate>
      <link>https://dev.to/3theye/use-kotlin-22bf</link>
      <guid>https://dev.to/3theye/use-kotlin-22bf</guid>
      <description>&lt;p&gt;คำสั่งในการอ่านไฟล์แบบเดิมนั้นค่อนข้างยาวและเข้าใจยาก ตามตัวอย่างด้านล่าง&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val CustomReader by lazy {
    val file = File(cacheDir, fileName)
    val outputStream = FileOutputStream(file)
    try {
       val inputStream = assets.open(fileName)
       var bytesCopied: Long = 0
       val buffer = ByteArray(8 * 1024)
       var bytes = inputStream.read(buffer)
       while (bytes &amp;gt;= 0) {
           outputStream.write(buffer, 0, bytes)
           bytesCopied += bytes
           bytes = inputStream.read(buffer)
       }
    } finally {
       outputStream.close()
    }
    CustomReader(file)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ใน Kotlin 1.2 มี extension function สำหรับ Closeable ที่เรียกว่า &lt;em&gt;use&lt;/em&gt; โดยเจ้าคำสั่งนี้จะช่วยอำนวยความสะดวกให้เรา ทำให้เราไม่ต้องตาม try finally close() เหมือนแต่ก่อนอีกต่อไป.&lt;/p&gt;

&lt;p&gt;ด้วยคำสั่งนี้เราสามารถลดความซับซ้อนของ code ด้านบนได้อย่างง่ายดาย ดั่งตัวอย่างด้านล่างนี้เลย&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val CustomReader by lazy {
    val file = File(cacheDir, fileName)
    val outputStream = FileOutputStream(file)
    outputStream.use { outputStream -&amp;gt;
        val inputStream = assets.open(fileName)
        var bytesCopied: Long = 0
        val buffer = ByteArray(8 * 1024)
        var bytes = inputStream.read(buffer)
        while (bytes &amp;gt;= 0) {
            outputStream.write(buffer, 0, bytes)
            bytesCopied += bytes
            bytes = inputStream.read(buffer)
        }
    }
    CustomReader(file)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;แต่ยังไม่หมด เราสามารถลด code ได้อีกด้วย extension function อีกสองตัวคือ&lt;/p&gt;

&lt;h5&gt;
  
  
  copy-to
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val CustomReader by lazy {
    val file = File(cacheDir, fileName)
    val outputStream = FileOutputStream(file)
    outputStream().use { fileOut -&amp;gt;
        assets.open(fileName).copyTo(fileOut)
    }
    CustomReader(file)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  outputStream
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val CustomReader by lazy {
    val file = File(cacheDir, fileName)
    file.outputStream().use { fileOut -&amp;gt;
        assets.open(fileName).copyTo(fileOut)
    }
    CustomReader(file)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
