PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/installer/InstallThread.java

#
Java | 143 lines | 95 code | 25 blank | 23 comment | 9 complexity | cb498de2234ec66eedd95401aef9ab13 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * InstallThread.java
  3. * Copyright (C) 1999, 2000 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package installer;
  20. import java.io.*;
  21. import java.util.Vector;
  22. /*
  23. * The thread that performs installation.
  24. */
  25. public class InstallThread extends Thread
  26. {
  27. public InstallThread(Install installer, Progress progress,
  28. String installDir, String binDir, int size, Vector components)
  29. {
  30. super("Install thread");
  31. this.installer = installer;
  32. this.progress = progress;
  33. this.installDir = installDir;
  34. this.binDir = binDir;
  35. this.size = size;
  36. this.components = components;
  37. buf = new byte[32768];
  38. }
  39. public void setProgress(Progress progress)
  40. {
  41. this.progress = progress;
  42. }
  43. public void run()
  44. {
  45. progress.setMaximum(size * 1024);
  46. try
  47. {
  48. for(int i = 0; i < components.size(); i++)
  49. {
  50. installComponent((String)components.elementAt(i));
  51. }
  52. // create it in case it doesn't already exist
  53. if(binDir != null)
  54. OperatingSystem.getOperatingSystem().mkdirs(binDir);
  55. OperatingSystem.getOperatingSystem().createScript(
  56. installer,installDir,binDir,
  57. installer.getProperty("app.name"));
  58. }
  59. catch(FileNotFoundException fnf)
  60. {
  61. progress.error("The installer could not create the "
  62. + "destination directory.\n"
  63. + "Maybe you do not have write permission?");
  64. return;
  65. }
  66. catch(IOException io)
  67. {
  68. progress.error(io.toString());
  69. return;
  70. }
  71. progress.done();
  72. }
  73. // private members
  74. private Install installer;
  75. private Progress progress;
  76. private String installDir;
  77. private String binDir;
  78. private int size;
  79. private Vector components;
  80. private byte[] buf;
  81. private void installComponent(String name) throws IOException
  82. {
  83. BufferedReader fileList = new BufferedReader(
  84. new InputStreamReader(getClass()
  85. .getResourceAsStream(name)));
  86. String fileName;
  87. while((fileName = fileList.readLine()) != null)
  88. {
  89. String outfile = installDir + File.separatorChar
  90. + fileName.replace('/',File.separatorChar);
  91. InputStream in = new BufferedInputStream(
  92. getClass().getResourceAsStream("/" + fileName));
  93. if(in == null)
  94. throw new FileNotFoundException(fileName);
  95. copy(in,outfile);
  96. in.close();
  97. }
  98. fileList.close();
  99. }
  100. private void copy(InputStream in, String outfile) throws IOException
  101. {
  102. File outFile = new File(outfile);
  103. OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
  104. BufferedOutputStream out = new BufferedOutputStream(
  105. new FileOutputStream(outFile));
  106. int count;
  107. for(;;)
  108. {
  109. count = in.read(buf,0,buf.length);
  110. if(count == -1)
  111. break;
  112. out.write(buf,0,count);
  113. progress.advance(count);
  114. }
  115. in.close();
  116. out.close();
  117. }
  118. }