gt:         int lastTingID = ouf.readInt();
context: /*
      Copyright (C) 2015   Martin Dames <martin@bastionbytes.de>
    
      This program is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation; either version 2 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License along
      with this program; if not, write to the Free Software Foundation, Inc.,
      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    
  */
  
  package tingeltangel.core;
  
  import java.io.BufferedReader;
  import java.io.ByteArrayOutputStream;
  import java.io.DataInputStream;
  import java.io.File;
  import java.io.InputStream;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.StringReader;
  import java.nio.file.Files;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.Map;
  import org.apache.log4j.LogManager;
  import org.apache.log4j.Logger;
  import tingeltangel.core.constants.ScriptFile;
  import tingeltangel.core.constants.TxtFile;
  import tingeltangel.core.scripting.Command;
  import tingeltangel.core.scripting.Commands;
  import tingeltangel.core.scripting.SyntaxError;
  import tingeltangel.tools.FileEnvironment;
  import tingeltangel.tools.ProgressDialog;
  
  
  public class Importer {
  
      private final static Logger log = LogManager.getLogger(Importer.class);
      
      /**
       * 
       * @param oufFile
       * @param txt (could be null)
       * @param scriptFile (could be null)
       * @param book
       * @throws Exception 
       */
      public static void importBook(File oufFile, Map<String, String> txt, File scriptFile, File pngFile, Book book, ProgressDialog progress) throws IOException, SyntaxError {
  
          if(txt != null) {
              book.setAuthor(txt.get(TxtFile.KEY_AUTHOR));
              book.setName(txt.get(TxtFile.KEY_NAME));
              book.setPublisher(txt.get(TxtFile.KEY_PUBLISHER));
              book.setURL(txt.get(TxtFile.KEY_URL));
              book.setVersion(Integer.parseInt(txt.get(TxtFile.KEY_VERSION)));
          } else {
              book.setAuthor("unknown");
              book.setName("unknown");
              book.setPublisher("unknown");
              book.setURL("unknown");
              book.setVersion(1);
          }
          
          if(pngFile != null) {
              // copy file from "pngFile" to "book.getCover()"
              InputStream input = null;
              OutputStream output = null;
              try {
                  input = new FileInputStream(pngFile);
                  output = new FileOutputStream(book.getCover());
                  byte[] buf = new byte[1024];
                  int bytesRead;
                  while ((bytesRead = input.read(buf)) > 0) {
                      output.write(buf, 0, bytesRead);
                  }
              } finally {
                  if(input != null) {
                      input.close();
                  }
                  if(output != null) {
                      output.close();
                  }
              }
          }
          
          // load script file
          HashMap<Integer, String> scripts = new HashMap<Integer, String>();
          HashMap<Integer, String> notes = new HashMap<Integer, String>();
          HashMap<Integer, Boolean> scriptEntryIsMP3 = new HashMap<Integer, Boolean>();
          if(scriptFile != null) {
          
              BufferedReader in = new BufferedReader(new FileReader(scriptFile));
              String row;
              boolean inScript = true;
              boolean inNote = false;
              int precode = -1;
              String script = "";
              String note = "";
              while((row = in.readLine()) != null) {
                  if(inNote) {
                      if(row.startsWith(ScriptFile.CONTENT)) {
                          notes.put(precode, note);
                          note = "";
                          inNote = false;
                          inScript = true;
                      } else if(row.startsWith(ScriptFile.PRECODE)) {
                          if(precode != -1) {
                              notes.put(precode, note);
                          }
                          note = "";
                          inNote = false;
                          precode = Integer.parseInt(row.substring(ScriptFile.PRECODE.length()).trim());
                      } else {
                          note += row + ScriptFile.LB;
                      }
                  } else if(inScript) {
                      if(row.startsWith(ScriptFile.PRECODE)) {
                          if(precode != -1) {
                              scripts.put(precode, script);
                          }
                          script = "";
                          inScript = false;
                          precode = Integer.parseInt(row.substring(ScriptFile.PRECODE.length()).trim());
                      } else {
                          script += row + ScriptFile.LB;
                      }
                  } else {
                      if(row.startsWith(ScriptFile.CONTENT)) {
                          inScript = true;
                      } else if(row.toUpperCase().startsWith("TYPE=")) {
                          String type = row.substring("TYPE=".length()).trim();
                          if(type.equals("1")) {
                              scriptEntryIsMP3.put(precode, true);
                          } else {
                              scriptEntryIsMP3.put(precode, false);
                          }
                      } else if(row.startsWith(ScriptFile.NOTE)) {
                          inNote = true;
                      }
                  }
                  if(inNote) {
                      notes.put(precode, note);
                  } else if(inScript) {
                      if(precode != -1) {
                          scripts.put(precode, script);
                      }
                  }
              }
              in.close();
          }
          
          if((oufFile == null) && (scriptFile != null)) {
              Iterator<Integer> ids = scripts.keySet().iterator();
              while(ids.hasNext()) {
                  int id = ids.next();
                  
                  book.addEntry(id);
                  Entry entry = book.getEntryByOID(id);
                  
                  if(scriptEntryIsMP3.get(id)) {
                      entry.setMP3();
                      
                      BufferedReader br = new BufferedReader(new StringReader(scripts.get(id)));
                      String row;
                      String filename = "\n";
                      while((row = br.readLine()) != null) {
                          row = row.trim();
                          if(!row.isEmpty()) {
                              filename = row;
                              break;
                          }
                      }
                      
                      File src = new File(filename);
                      
                      if(src.canRead()) {
                          File target = new File(FileEnvironment.getAudioDirectory(book.getID()), src.getName());
  
                          target.getParentFile().mkdirs();
                          FileEnvironment.copy(src, target);
                          entry.setMP3(target);
                          entry.setHint(src.getName());
                      }
                  } else {
                      Script script = new Script(scripts.get(id), entry);
                      entry.setScript(script);
                      if(script.isSub()) {
                          entry.setSub();
                      } else {
                          entry.setCode();
                      }
                  }
                  String note = notes.get(id);
                  if(note != null) {
                      entry.setHint(note);
                  }
  
              }
              System.out.println("save book");
              book.save();
              return;
          }
          
          
          DataInputStream ouf = new DataInputStream(new FileInputStream(oufFile));
          
          int startOfIndex = ouf.readInt();
          if(startOfIndex != 0x66) { // why ?
              ouf.readInt(); // 2
          } 
          int firstTingID = ouf.readInt();
  