package com.marinilli.b2.c11;
import com.marinilli.b2.c11.util.*;
import javax.jnlp.*;
import java.io.*;
/**
 * Chapter 11
 * @author Mauro Marinilli
 * @version 1.0
 */
public class FileContentsExample {
  public FileContentsExample() {
    testRead();
    testWrite();
    writeAsRAF();
    System.exit(0);
  }
  private void testRead() {
    FileContents file = null;
    FileOpenService fs =
     (FileOpenService)Utilities.getService("javax.jnlp.FileOpenService");
    if (fs!=null) {
      try {
        file = fs.openFileDialog(null, null);
      } catch (Exception e) {
        System.out.println("openFile: "+e);
      }
    }
    if (file!=null) {
      process(file);
      readLines(file);
    }
    System.out.println("finished.");
  }
  private void testWrite() {
    System.out.println("testWrite()");
    FileContents file = null;
    FileSaveService fs =
     (FileSaveService)Utilities.getService("javax.jnlp.FileSaveService");
    if (fs!=null) {
      try {
        file =
         ((FileOpenService)Utilities.getService("javax.jnlp.FileOpenService"))
            .openFileDialog(null, null);
        FileContents outcome = fs.saveAsFileDialog(null,null,file);
        System.out.println("outcome "+outcome);
      } catch (Exception e) {
        System.out.println("openFile: "+e);
      }
    }
    if (file!=null) {
      writeData(file);
    }
  }
  private void testWrite2() {
    FileContents file = null;
    FileSaveService fs =
     (FileSaveService)Utilities.getService("javax.jnlp.FileSaveService");
    if (fs!=null) {
      try {
        FileContents outcome = fs.saveAsFileDialog(null,null,file);
      } catch (Exception e) {
        System.out.println("openFile: "+e);
      }
    }
    if (file!=null) {
      writeData(file);
    }
  }
  public void readLines(FileContents file){
    try {
      InputStream input = file.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
      String s = "";
      while ((s = reader.readLine()) != null) {
        System.out.println("\""+s+"\"");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public byte[] readBytes(FileContents file){
    byte [] buffer = null;
    try {
      InputStream input = file.getInputStream();
      buffer = new byte[(int)file.getLength()];
      input.read(buffer);
      input.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return buffer;
  }
  public void process(FileContents file){
    try {
      InputStream input = file.getInputStream();
      int c;
      while ((c = input.read())!= -1) {
         System.out.write(c);
      }
      input.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void writeData(FileContents file){
    try {
      if (file.canWrite()) {
        DataOutputStream dos = new DataOutputStream(file.getOutputStream(false));
        dos.writeInt(123);
        dos.writeFloat(123F);
        dos.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void writeAsRAF(){
    FileContents file = null;
    FileOpenService fs =
     (FileOpenService)Utilities.getService("javax.jnlp.FileOpenService");
    if (fs!=null) {
      try {
        file=fs.openFileDialog(null,null);
        if (file.canWrite()) {
          enlargeFile(file, 2028);
          if (file.getMaxLength() > file.getLength() ) {
           JNLPRandomAccessFile raf = file.getRandomAccessFile("rw");
           raf.seek(raf.length() - 1);
           raf.writeUTF("Last In file.");
           raf.close();
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private long enlargeFile(FileContents file, int extraLength){
    long length = 0L;
    if (file!=null)
      try {
        length = file.getLength();
        if (length + extraLength > file.getMaxLength()) {
           // attempt to increase the maximum file size defined by the client
           length = file.setMaxLength(length + 1024);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    return length;
  }
  public static void main(String[] args) {
    FileContentsExample fileContentsExample1 = new FileContentsExample();
  }
}