Showing posts with label application. Show all posts
Showing posts with label application. Show all posts

Tuesday, November 14, 2017

Java: How to create zip archive from byte array data.

Beside create compressed archive from files, We can create from byte array that is stored in memory and not necessary to write byte array down to disk before create it. For this reason, the application will have better performance because less I/O operation.

The following tutorial, we show how to create zip archive from byte array.

1. Add dependency.
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-compress</artifactId>
           <version>1.15</version>
</dependency>
2. Implement The following example code.

2.1 Write to file.

package com.haruka.compressfilebytearraydemo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;



public class App

{

    public static void main( String[] args )

    {

     ZipArchiveOutputStream zOutputStream = null;

     FileOutputStream fileOutputStream = null;

     try {

      List<byte[]> byteArrayListToWrite = new ArrayList<byte[]>();

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 1".getBytes());

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 2".getBytes());

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 3".getBytes());



      fileOutputStream = new FileOutputStream("C:\\DEMO\\bytearrayarchivedemo.zip"); //path to save archive file.

      zOutputStream = new ZipArchiveOutputStream(fileOutputStream);

      

      for(int i=0;i<byteArrayListToWrite.size();i++) {

       byte[] dataToWrite = byteArrayListToWrite.get(i);

       ZipArchiveEntry zEntry = new ZipArchiveEntry("file_"+i+".txt"); // create zip entry and specific file name.

       zEntry.setSize(dataToWrite.length); //set size of data.
       zOutputStream.putArchiveEntry(zEntry); //add entry.

       zOutputStream.write(dataToWrite);

       zOutputStream.closeArchiveEntry(); // commit added entry (commit file).

      }

     }catch(Exception ex) {

      ex.printStackTrace();

     }finally {

       try {

        if(zOutputStream != null) {

         zOutputStream.flush(); //save file.

         zOutputStream.close();

        }

        if(fileOutputStream != null) {

         fileOutputStream.close();

        }

    } catch (IOException e) {

     e.printStackTrace();

    }

     }

    }

}

 2.2 Write to servlet response.
package com.haruka.compressfilebytearraydemoservlet.servlet;



import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;



public class CompressFileByteArrayDemo extends HttpServlet{

 @Override

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  ZipArchiveOutputStream zOutputStream = null;

     try {

      List<byte[]> byteArrayListToWrite = new ArrayList<byte[]>();

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 1".getBytes());

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 2".getBytes());

      byteArrayListToWrite.add("THIS IS DATA TO TEST WHRITE ZIP WITH BYTE ARRAY 3".getBytes());

      response.setContentType("application/zip"); //set content type

      zOutputStream = new ZipArchiveOutputStream(response.getOutputStream());

      

      for(int i=0;i<byteArrayListToWrite.size();i++) {

       byte[] dataToWrite = byteArrayListToWrite.get(i);

       ZipArchiveEntry zEntry = new ZipArchiveEntry("file_"+i+".txt"); // create zip entry and specific file name.

       zEntry.setSize(dataToWrite.length); //set size of data.

       zOutputStream.putArchiveEntry(zEntry); //add entry.

       zOutputStream.write(dataToWrite);

       zOutputStream.closeArchiveEntry(); // commit added entry (commit file).

      }

     }catch(Exception ex) {

      ex.printStackTrace();

     }finally {

       try {

        if(zOutputStream != null) {

         zOutputStream.flush(); //save file.

         zOutputStream.close();

        }

    } catch (IOException e) {

     e.printStackTrace();

    }

     }

 }

}

Furthermore, the example can be adapt with other outputStream for any purpose.

The example can be downloaded in following github:

1. For write to file example.
https://github.com/benzyaa/javademo/tree/master/compressfilebytearraydemo

2. For write to servlet response.
https://github.com/benzyaa/javademo/tree/master/compressfilebytearraydemoservlet

Further Information: 

https://commons.apache.org/proper/commons-compress/index.html

https://mvnrepository.com/artifact/org.apache.commons/commons-compress

Friday, August 25, 2017

Anime Quiz - Let test your anime knowledge for learning about Japanese Anime pop culture.



Anime Quiz is android application let you to test yourself about anime knowledge. It has updated quiz pack that you can select for testing. Quiz packs are time challenge that offer you  experience, rewards, and anime knowledge improvement.

When you try this application, it let you to inform your name for displaying for registration, the registration enable you to get experience and rewards. After registration you will get at least 20,000 coins for purchasing item that can be used for helping you in quiz session.

Before play it, you can purchase item in item store. In this session, you can see list of items those are purchased in inventory. You can select Store for item purchasing. And coins will be used for payment.

When you play it, you will see quiz packs that you can select for quiz session. Every quiz session are time challenge quiz. The quiz will be stared when you press play it of quiz packs. During quiz session, the stopwatch will count down until time up. In this session you can select items those are bought in item store for helping you in this session. When time up, the summary dialog will be appear, the summary is include total question, total right answer, total wrong answer, not answered question, gained experience, and earned coins.
In that dialog, you can press review result for reviewing what you select answers.

Anime quiz expect you to know what you actual know in Anime world and let you to improve your anime knowledge. Not only anime knowledge, The quiz will offer quiz packs that concern about Japanese pop culture and Basic Japanese Language.

Anime quiz is now available on Google Play.
Let download and try to test yourself now !!

Get it on Google Play








If you use android smartphone, press below:
   
Get it on Google Play

Or get by QR Code












 


The character of homepage and Icon is drawn by me see full image at PIXIV on below link:
https://www.pixiv.net/member_illust.php?mode=medium&illust_id=6458654

 The briefly screenshot of application.


Thursday, September 29, 2016

ทำไมต้องใช้ MVVM Design pattern สำหรับ การพัฒนา Application

การพัฒนา Application ในปัจจุบัน โดยเฉพาะ Application ประเภท Web application นิยมนำ MVC (Model View Controller) Design Pattern มาปรับใช้ เพิื่อความสะดวกในการ เพิ่มเติม หรือ แก้ไข ในส่วนต่าง ๆ ของ Application  โดยการปรับใช้  MVC สามารถอธิบายได้ดังนี้

1. Model – เป็นส่วนเกี่ยวกับการจัดการข้อมูลทั่วไป ได้แก่ ส่วนที่จัดการเกี่ยวกับ Database, File, Web Service เป็นต้น

2. View – เป็นส่วนที่ติดต่อกับผู้ใช้งาน โดยการแสดงข้อมูลที่นำมาจาก Model หรือ Controllerและ เป็นส่วนที่รับการตอบสนองต่าง ๆ จาก User เช่น การกดเลือกเมนู การ Submit Form ส่วนนี้ได้แก่ หน้าจอแบบต่าง ๆ ของ Application เช่น หน้าเว็บ, Windows Form เป็นต้น

3. Controller – เป็นส่วนสำหรับการทำงานของ Application ตาม Business Logic ที่ได้กำหนดไว้ โดยส่วนนี้จะติดต่อกับ View เพื่อรับข้อมูล และ ส่งข้อมูลกลับไปเมื่อ Controller ทำงานเสร็จ อีกทั้ง ยังติดต่อกับส่วน Model เพื่อดึงข้อมูลสำหรับการทำงาน และ บันทึกหรือเปลี่ยนแปลงข้อมูล ภายหลังการทำงานเสร็จ ในส่วนนี้ สามารถยกตัวอย่างได้ เช่น Servlet (ในเทคโนโลยีกลุ่ม Java), Action (ใน Struts Web Framework) เป็นต้น



MVC Design pattern ยอมให้ View สามารถติดต่อกับ Model ได้โดยตรง เพื่อความสะดวกในการ ดึงข้อมูล บันทึกข้อมูล หรือ แก้ไขข้อมูล โดยไม่ต้องพึ่งพา Controller แต่อย่างใด จึงทำให้ Developer เลือกที่จะเขียน Code เพื่อที่จะ รับ/ส่ง ข้อมูลโดยตรง ระหว่าง View และ Model ขอยกกรณีตัวอย่างเช่น

การเขียน Code ให้เมื่อมีการ Submit Form เกิดขึ้น จะมีการสร้าง Request ไปยังส่วนบันทึกข้อมูล เพื่อรันคำสั่ง SQL ให้บันทึกข้อมูลที่รับมาจาก User  และให้ View ทำการแสดงข้อมูลที่บันทึกแล้ว และ แสดงข้อความสถานะว่า บันทึกข้อมูลเรียบร้อยแล้ว ภายหลัง บันทึกข้อมูลเสร็จสิ้น

จากกรณีดังกล่าว มีข้อเสียเปรียบดังนี้
1. หากมีการแก้ไขการทำงาน ของ Model จำเป็นต้องแก้ไข View ด้วย
2. Developer จำเป็นต้องเขียน Code ในส่วน View เพื่อควบคุมการแสดงผลข้อมูล เมื่อมีการกระทำใด ๆ ระหว่าง View และ Model

และเพื่อลดข้อเสียเปรียบดังกล่าว จึงได้มีการพัฒนา  MVVM(Model View Viewmodel) Design Pattern ซึ่งต่อยอดมาจาก MVC Design pattern โดยมีจุดประสงค์ดังนี้

1. ไม่ยอมให้ View สามารถติดต่อกับ Model โดยตรง
2. ลด Code ในส่วนการควบคุมการแสดงผลของหน้า View เมื่อมีการเปลี่ยนแปลงของ Model ให้ได้มากที่สุด
3. หาก View ต้องการดึงข้อมูล จะต้องกระทำผ่าน ViewModel เท่านั้น

MVVM Design Pattern จะตัดความสัมพันธ์โดยตรงระหว่าง View และ Model เพื่อความเป็นระเบียบในการจัดการข้อมูลต่าง ๆ ใน Model และ เพิ่มความสามารถของ Controller ให้สามารถจัดการการรับข้อมูลจาก View และการแสดงผลข้อมูลในส่วนของ View  Controller ที่ถูกเพิ่มความสามารถนี้ เรียกว่า ViewModel สามารถอธิบายได้ดังแผนภาพต่อไปนี้


ในการทำงานของ Application ที่ถูกพัฒนาด้วย MVVM นั้น เมื่อมีการเปลี่ยนแปลงในส่วน View เกิดขึ้น จะมีการแจ้งไปยัง ViewModel ว่ามีการเปลี่ยนแปลงแบบใดบ้าง และ ViewModel จะตอบสนองต่อการเปลี่ยนแปลงนั้น ๆ

และในทางปฏิบัติ จะต้องมี Binder อยู่ระหว่าง View และ ViewModel เพื่อควบคุมการติดต่อระหว่างสองส่วนนี้ให้มีความถูกต้องแม่นยำ โดยที่ Binder จะจับตาการเปลี่ยนแปลงของ View และ ViewModel และเมื่อมีการเปลี่ยนแปลง จะมีการแจ้งข้อมูลการเปลี่ยนแปลงถึงกันและกัน

ขอยกกรณีตัวอย่างเช่น

Application สำหรับเก็บข้อมูลลูกค้า เมื่อมีการเลือกจังหวัดใน Form แล้วจะต้องแสดง ตัวเลือกอำเภอ ให้ผู้ใช้งานเลือก

สามารถอธิบายการทำงานได้ดังนี้

เมื่อ ผู้ใช้งานเลือกจังหวัดใน Form เช่น จังหวัดเชียงราย ซึ่งอยู่ในส่วน View แล้ว จะมีการแจ้งเตือนไปยัง ViewModel ว่ามีการเปลี่ยนแปลงให้หน้า View  คือมีการเปลี่ยนแปลงจังหวัดที่เลือก เป็น จังหวัดเชียงราย เมื่อ ViewModel ได้รับข้อมูลแล้ว จะแจ้งให้ Modelทำการ Query ข้อมูลอำเภอ ของจังหวัดเชียงราย และส่งกลับไปยัง ViewModel จากนั้น ViewModel จะแจ้งให้ View เพื่อให้เปลี่ยนแปลงตัวเลือก อำเภอ ให้เป็นรายชื่ออำเภอในจังหวัดเชียงราย


Framework สำหรับการพัฒนา Application โดยใช้ MVVM ที่นิยมใช้กันทั่วไป ได้แก่ ZK Framework สำหรับ Java WebApplication, WPF(Window Presentation Foundation) สำหรับ การพัฒนา Application โดยใช้เทคโนโลยีของ Microsoft, Knocout เป็น Javascript สำหรับพัฒนา Web Application แบบ MVVM

ข้อดีของการพัฒนา Application โดยใช้ MVVM สามารถสรุปได้ดังนี้คือ

1. สามารถสร้างหน้า View ได้หลากหลายรูปแบบ เช่น สำหรับ Desktop หรือ สำหรับ Mobile ได้โดยใช้ ViewModel   เดียวกันได้

2. เหมาะสำหรับการพัฒนา Application ที่มีการเปลี่ยนแปลง User Interface บ่อยครั้ง เนื่องจากทุกการเปลี่ยนแปลงในส่วนนี้ จะไม่กระทบต่อ ViewModel

3. ไม่จำเป็นต้องเขียน Code ในการควบคุม View เพื่อรองรับ การเปลี่ยนแปลงของข้อมูลใน View หรือใน ViewModel เนื่องจาก มี Binder ช่วยจัดการให้แล้ว

4. รองรับการพัฒนา Application เพื่อให้ตอบสนองกับ User แบบ Real-time ได้ง่ายเนื่องจาก ทุกการกระทำในส่วนของ View จะถูกแจ้งไปยัง ViewModel

5. เหมาะสำหรับการพัฒนา Application ที่ต้องบันทึกการกระทำของ User ในส่วนของ View เช่น Application สำหรับการชำระเงินผ่าน Internet


แต่อย่างไรก็ตาม MVVM ยังมีข้อด้อยคือ

1. Client ต้องส่ง Request ไปยัง Server บ่อยครั้งเพื่อแจ้งให้ ViewModel ทราบถึงการกระทำในฝั่ง Client


การพัฒนา Application ในปัจจุบันนี้ จำเป็นต้องตอบสนองการเปลี่ยนแปลงที่เกิดขึ้นบ่อยครั้ง โดยเฉพาะในสิ่งที่เกี่ยวกับ User Interface และ User Experience ให้ได้รวดเร็วที่สุด และ Application ที่จะถูกพัฒนาในอนาคตจะต้องสามารถตอบสนองต่อ User แบบ Real-time ได้ ดังนั้นแล้ว MVVM Design pattern อาจเป็นสิ่งหนึ่งที่จะต้องถูกพิจารณาสำหรับการพัฒนา Application ในลำดับถัดไป

Wednesday, October 14, 2015

Math Equation Quiz - Let sharpen your mind with mathermatic equation.

When you want to try to sharpen your mind, The matermatic equation solving is one of the alternative for your mind sharpening. The Math Equation Quiz is android application that let you to sharpen your mind with mathermatic equation.

The appplication challenge your mind in mathermatic skill by incomplete mathermatic equation. When you try this application, It will show the incomplete equation with the blank slot and you have find for the appropritate number or mathermatic operation for completing the equation. You will get 10 points when equation is solved and you can go forward for the next equation, otherwise the quiz will reset the equation and selected numbers or operations. You can also select the option are quiz item for spending for quiz item, reset button to reset choice and hint button to let quiz for spending quiz credit to let quiz select the appropriate number or mathermatic operation to put in equation bucket.

During the quiz session you can apply the mathermatics real number theory to solve the incomplete equation, the challenge of the equation solving is depend on your solved equation number and your reached levels.

Moremover, the application has social network sharing on facebook, so let share this application to each other for more equation solving experiencs.

Let prepare your mind and knowlege and get it on Google play store



Or read QR Code for downloading.

mathequiz qrcode

And brifly screenshot.

Screenshot 2015 10 10 20 55 38Screenshot 2015 10 10 20 55 47Screenshot 2015 10 10 20 56 21Screenshot 2015 10 10 20 56 25Screenshot 2015 10 10 20 56 58Screenshot 2015 10 10 20 57 09

And please visit our Facebook pages at MOEDojo