client/ftpd/src/se/lu/thep/coreftpd/common/StreamIO.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic, Olle Mansson
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25 //  Xerver Free Web Server
741 10 Oct 06 olle 26 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 27 //
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 30 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 31 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 32 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 33 //
741 10 Oct 06 olle 34 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 35 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 36 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 37 //  GNU General Public License for more details.
741 10 Oct 06 olle 38 //
741 10 Oct 06 olle 39 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 40 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 41 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 42 //
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //  #############################################################
741 10 Oct 06 olle 45 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 46 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 47 //  ##                                                         ##
741 10 Oct 06 olle 48 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 49 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 50 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 51 //  ##                                                         ##
741 10 Oct 06 olle 52 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 53 //  #############################################################
1652 22 May 07 gregory 54 package se.lu.thep.coreftpd.common;
741 10 Oct 06 olle 55
790 23 Oct 06 olle 56 import org.proteios.core.DbControl;
790 23 Oct 06 olle 57 import org.proteios.core.Directory;
1656 22 May 07 gregory 58 import org.proteios.core.ItemFactory;
3389 06 Aug 09 olle 59 import org.proteios.core.MimeType;
790 23 Oct 06 olle 60 import org.proteios.core.Path.Type;
1676 25 May 07 olle 61 import org.proteios.core.SessionControl;
1216 18 Feb 07 gregory 62 import se.lu.thep.coreftpd.ftp_server.FTPNewConnection;
1216 18 Feb 07 gregory 63 import java.io.File;
1216 18 Feb 07 gregory 64 import java.io.InputStream;
1216 18 Feb 07 gregory 65 import java.io.OutputStream;
741 10 Oct 06 olle 66
741 10 Oct 06 olle 67 /**
1652 22 May 07 gregory 68  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 69  * <CODE>StreamIO</CODE> contains only static classes. <BR>
741 10 Oct 06 olle 70  * By the name of the members you are supposed to understand what they do. ;)
1652 22 May 07 gregory 71  * 
741 10 Oct 06 olle 72  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 73  * @version 1.0
741 10 Oct 06 olle 74  */
1656 22 May 07 gregory 75 final public class StreamIO
1656 22 May 07 gregory 76 {
1652 22 May 07 gregory 77   final static private boolean b_showErrors = false;
1052 08 Dec 06 olle 78   final static private char coreSeparatorChar = FTPNewConnection.coreSeparatorChar;
1052 08 Dec 06 olle 79   final static private String coreSeparator = FTPNewConnection.coreSeparator;
790 23 Oct 06 olle 80   /**
790 23 Oct 06 olle 81    * Logger used. Used to log specific events.
790 23 Oct 06 olle 82    */
790 23 Oct 06 olle 83   private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
1656 22 May 07 gregory 84     .getLogger("se.lu.thep.coreftpd.common");
741 10 Oct 06 olle 85
1656 22 May 07 gregory 86
1656 22 May 07 gregory 87   static public void writeStreamToStream(InputStream is, OutputStream os)
1656 22 May 07 gregory 88   {
1656 22 May 07 gregory 89     try
1656 22 May 07 gregory 90     {
741 10 Oct 06 olle 91       int n;
1652 22 May 07 gregory 92       byte[] buffer = new byte[8192];
1656 22 May 07 gregory 93       while ((n = is.read(buffer)) != -1)
1656 22 May 07 gregory 94       {
1652 22 May 07 gregory 95         os.write(buffer, 0, n);
741 10 Oct 06 olle 96       }
741 10 Oct 06 olle 97       os.flush();
1656 22 May 07 gregory 98     }
1656 22 May 07 gregory 99     catch (Exception e)
1656 22 May 07 gregory 100     {
1652 22 May 07 gregory 101       if (b_showErrors)
1652 22 May 07 gregory 102         System.out
1656 22 May 07 gregory 103           .println("An error has occured @ writeStreamToStream:\n" + e);
1652 22 May 07 gregory 104     }
741 10 Oct 06 olle 105   }
741 10 Oct 06 olle 106
1656 22 May 07 gregory 107
2418 22 Nov 07 gregory 108   static public void writeFileToStream(File theFile, OutputStream os,
2418 22 Nov 07 gregory 109       SessionControl sc)
1656 22 May 07 gregory 110   {
1676 25 May 07 olle 111     writeFileToStream(theFile, os, 0, sc);
741 10 Oct 06 olle 112   }
741 10 Oct 06 olle 113
1656 22 May 07 gregory 114
1652 22 May 07 gregory 115   static public void writeFileToStream(File theFile, OutputStream os,
1676 25 May 07 olle 116       int i_startPosition, SessionControl sc)
1656 22 May 07 gregory 117   {
790 23 Oct 06 olle 118     // *** Debug Info
1656 22 May 07 gregory 119     log
1656 22 May 07 gregory 120       .info("StreamIO::writeFileToStream(): Start - theFile = \"" + theFile + "\" i_startPosition = " + i_startPosition);
799 24 Oct 06 olle 121     /*
799 24 Oct 06 olle 122      * Remove directory name from path to get pure filename
799 24 Oct 06 olle 123      */
1652 22 May 07 gregory 124     // String
1652 22 May 07 gregory 125     // s_theFileDir=s_theFile.substring(0,s_theFile.lastIndexOf(File.separatorChar)+1);
799 24 Oct 06 olle 126     String s_theFileName = theFile.getName();
1656 22 May 07 gregory 127     log
1656 22 May 07 gregory 128       .info("StreamIO::writeFileToStream(): s_theFileName = \"" + s_theFileName + "\"");
1052 08 Dec 06 olle 129     /*
1652 22 May 07 gregory 130      * Variable "theFile" is a java.io.File object, and its toString()
1652 22 May 07 gregory 131      * method will display the file path with file separator characters for
1652 22 May 07 gregory 132      * the local server system.
1052 08 Dec 06 olle 133      */
1052 08 Dec 06 olle 134     String s_theFileLocal = theFile.toString();
1652 22 May 07 gregory 135     String s_theFile = s_theFileLocal.replace(File.separatorChar,
1656 22 May 07 gregory 136       coreSeparatorChar);
1652 22 May 07 gregory 137     // String parentDirPath =
1652 22 May 07 gregory 138     // s_theFile.substring(0,s_theFile.lastIndexOf(File.separatorChar,
1652 22 May 07 gregory 139     // s_theFile.length()-2)+1);
1652 22 May 07 gregory 140     String parentDirPath = s_theFile.substring(0, s_theFile.lastIndexOf(
1656 22 May 07 gregory 141       coreSeparatorChar, s_theFile.length() - 2) + 1);
1656 22 May 07 gregory 142     log
1656 22 May 07 gregory 143       .info("StreamIO::writeFileToStream(): parentDirPath = \"" + parentDirPath + "\"");
1652 22 May 07 gregory 144     // FileInputStream fileStreamed=null;
790 23 Oct 06 olle 145     InputStream coreInputStream = null;
1652 22 May 07 gregory 146     // DbControl dc = FTPServer.getSessionControl().newDbControl();
1652 22 May 07 gregory 147     // int coreUserId = FTPServer.getSessionControl().getLoggedInUserId();
1652 22 May 07 gregory 148     // log.info ("StreamIO::writeFileToStream(): coreUserId = " +
1652 22 May 07 gregory 149     // coreUserId);
1652 22 May 07 gregory 150     // User coreUser = User.getById(dc, coreUserId);
1652 22 May 07 gregory 151     // log.info ("StreamIO::writeFileToStream(): coreUser.getName() = \"" +
1652 22 May 07 gregory 152     // coreUser.getName() + "\"");
1652 22 May 07 gregory 153     // Directory homeDir = coreUser.getHomeDirectory();
1652 22 May 07 gregory 154     // log.info("StreamIO::writeFileToStream(): homeDir.getName() = \"" +
1652 22 May 07 gregory 155     // homeDir.getName() + "\"");
1652 22 May 07 gregory 156     // log.info("StreamIO::writeFileToStream(): dc.isClosed() = " +
1652 22 May 07 gregory 157     // dc.isClosed());
1652 22 May 07 gregory 158     // log.info ("StreamIO::writeFileToStream(): dc.isConnected() = " +
1652 22 May 07 gregory 159     // dc.isConnected());
790 23 Oct 06 olle 160     /*
1652 22 May 07 gregory 161      * In the future, coreDir may be any accessible sub-directory to the
1652 22 May 07 gregory 162      * homeDir of the logged-in user.
790 23 Oct 06 olle 163      */
1652 22 May 07 gregory 164     // Directory coreDir = homeDir;
1652 22 May 07 gregory 165     org.proteios.core.Path coreDirPath = new org.proteios.core.Path(
1656 22 May 07 gregory 166       parentDirPath, Type.DIRECTORY);
2418 22 Nov 07 gregory 167     // DbControl dc = FTPServer.getSessionControl().newDbControl();
1676 25 May 07 olle 168     DbControl dc = sc.newDbControl();
1656 22 May 07 gregory 169     ItemFactory factory = new ItemFactory(dc);
799 24 Oct 06 olle 170     Directory coreDir = null;
1656 22 May 07 gregory 171     try
1656 22 May 07 gregory 172     {
1656 22 May 07 gregory 173       coreDir = factory.getByPath(coreDirPath);
1656 22 May 07 gregory 174     }
1656 22 May 07 gregory 175     catch (Exception e)
1656 22 May 07 gregory 176     {
1652 22 May 07 gregory 177       log
1656 22 May 07 gregory 178         .info("StreamIO::writeFileToStream(): Could not get parent core directory, return.");
1651 21 May 07 olle 179       /*
1651 21 May 07 olle 180        * Close DbControl.
1651 21 May 07 olle 181        */
1651 21 May 07 olle 182       dc.close();
799 24 Oct 06 olle 183       return;
799 24 Oct 06 olle 184     }
1652 22 May 07 gregory 185     log
1656 22 May 07 gregory 186       .info("StreamIO::writeFileToStream(): coreDir.getPath().toString() = \"" + coreDir
1656 22 May 07 gregory 187         .getPath().toString() + "\"");
1652 22 May 07 gregory 188     String coreFilePathStr = new String(makeSlashAtEnd(coreDir.getPath()
1656 22 May 07 gregory 189       .toString()) + theFile.getName());
1656 22 May 07 gregory 190     log
1656 22 May 07 gregory 191       .info("StreamIO::writeFileToStream(): coreFilePathStr = \"" + coreFilePathStr + "\"");
799 24 Oct 06 olle 192     org.proteios.core.File coreFile = null;
1652 22 May 07 gregory 193     org.proteios.core.Path coreFilePath = new org.proteios.core.Path(
1656 22 May 07 gregory 194       coreFilePathStr, Type.FILE);
1656 22 May 07 gregory 195     try
1656 22 May 07 gregory 196     {
1656 22 May 07 gregory 197       coreFile = factory.getByPath(coreFilePath, false);
1656 22 May 07 gregory 198     }
1656 22 May 07 gregory 199     catch (Exception e)
1656 22 May 07 gregory 200     {
1652 22 May 07 gregory 201       log
1656 22 May 07 gregory 202         .info("StreamIO::writeFileToStream(): Exception when trying to get coreFile from path: " + e);
790 23 Oct 06 olle 203     }
1652 22 May 07 gregory 204     log
1656 22 May 07 gregory 205       .info("StreamIO::writeFileToStream(): (orig) coreFile.getName() = \"" + coreFile
1656 22 May 07 gregory 206         .getName() + "\"");
790 23 Oct 06 olle 207     /*
790 23 Oct 06 olle 208      * Save core file item before getting downloadstream
790 23 Oct 06 olle 209      */
1652 22 May 07 gregory 210     // dc.saveItem(coreFile);
1656 22 May 07 gregory 211     try
1656 22 May 07 gregory 212     {
2418 22 Nov 07 gregory 213       coreInputStream = coreFile.getDownloadStream(i_startPosition);
1656 22 May 07 gregory 214     }
1656 22 May 07 gregory 215     catch (Exception e)
1656 22 May 07 gregory 216     {
1652 22 May 07 gregory 217       log
1656 22 May 07 gregory 218         .info("StreamIO::writeFileToStream(): Exception when trying to get download stream for coreFile: " + e);
790 23 Oct 06 olle 219     }
1652 22 May 07 gregory 220     // InputStream coreInputStream = coreFile.getDownloadStream(0);
1656 22 May 07 gregory 221     if (coreInputStream == null)
1656 22 May 07 gregory 222     {
790 23 Oct 06 olle 223       log.info("StreamIO::writeFileToStream(): coreInputStream == null");
1656 22 May 07 gregory 224     }
1656 22 May 07 gregory 225     else
1656 22 May 07 gregory 226     {
1652 22 May 07 gregory 227       log.info("StreamIO::writeFileToStream(): coreInputStream != null");
790 23 Oct 06 olle 228     }
1656 22 May 07 gregory 229     try
1656 22 May 07 gregory 230     {
1652 22 May 07 gregory 231       // setPriority(MIN_PRIORITY);
1652 22 May 07 gregory 232       int numberOfBytesThatWillBeSent = 0;
1652 22 May 07 gregory 233       // fileStreamed=new FileInputStream(theFile);
1652 22 May 07 gregory 234       // fileStreamed.skip(i_startPosition); //Skippa i_startPosition
1652 22 May 07 gregory 235       // bytes of data from the file
1652 22 May 07 gregory 236       // coreInputStream.skip(i_startPosition); //Skippa i_startPosition
1652 22 May 07 gregory 237       // bytes of data from the file
1652 22 May 07 gregory 238       byte[] myBuffer = new byte[8192];// 40960]; //2048*20 (i tested
1656 22 May 07 gregory 239       // and this value seems to give
1656 22 May 07 gregory 240       // a very high speed when you
1656 22 May 07 gregory 241       // download files)
1652 22 May 07 gregory 242       // while
1652 22 May 07 gregory 243       // ((numberOfBytesThatWillBeSent=fileStreamed.read(myBuffer))!=-1)
1652 22 May 07 gregory 244       while ((numberOfBytesThatWillBeSent = coreInputStream
1656 22 May 07 gregory 245         .read(myBuffer)) != -1)
1656 22 May 07 gregory 246       {
1652 22 May 07 gregory 247         os.write(myBuffer, 0, numberOfBytesThatWillBeSent);
741 10 Oct 06 olle 248       }
1652 22 May 07 gregory 249       // NO MORE TIMEOUT STUFF: yield();
1652 22 May 07 gregory 250       // NO MORE TIMEOUT STUFF: sleep(50); //Be safe, don't close before
1652 22 May 07 gregory 251       // all data has been sent
1652 22 May 07 gregory 252       // setPriority(NORM_PRIORITY);
1656 22 May 07 gregory 253     }
1656 22 May 07 gregory 254     catch (Exception e)
1656 22 May 07 gregory 255     {
741 10 Oct 06 olle 256       if (b_showErrors)
1656 22 May 07 gregory 257         System.out
1656 22 May 07 gregory 258           .println("An error occured @ writeFileToStream:\n" + e
1656 22 May 07 gregory 259             .getMessage());
741 10 Oct 06 olle 260     }
1656 22 May 07 gregory 261     try
1656 22 May 07 gregory 262     {
1652 22 May 07 gregory 263       // if (fileStreamed!=null) //If fileStreamed "holds" a file
1652 22 May 07 gregory 264       // {
1652 22 May 07 gregory 265       // os.flush();
790 23 Oct 06 olle 266       /*
1652 22 May 07 gregory 267        * Important: This must be reached no matter what! Even if an
1652 22 May 07 gregory 268        * exception occurs in the try block, this must be reached.
1652 22 May 07 gregory 269        * Otherwise the file will be locked by Xerver (locked by Java.exe)
1652 22 May 07 gregory 270        * until the garbage collector is run (and you don't know when it
1652 22 May 07 gregory 271        * will run) and detects that the file (the object "fileStreamed")
1652 22 May 07 gregory 272        * is no longer referenced from anywhere else and it release the
1652 22 May 07 gregory 273        * file by automatic. Until this happenes, no other application can
1652 22 May 07 gregory 274        * write to or rename this file (however, this problem is solved
1652 22 May 07 gregory 275        * with this line).
790 23 Oct 06 olle 276        */
1652 22 May 07 gregory 277       // fileStreamed.close();
1652 22 May 07 gregory 278       // }
1652 22 May 07 gregory 279       if (coreInputStream != null) // If coreInputStream "holds" a file
741 10 Oct 06 olle 280       {
741 10 Oct 06 olle 281         os.flush();
790 23 Oct 06 olle 282         /*
1652 22 May 07 gregory 283          * Important: This must be reached no matter what! Even if an
1652 22 May 07 gregory 284          * exception occurs in the try block, this must be reached.
1652 22 May 07 gregory 285          * Otherwise the file will be locked by Xerver (locked by
1652 22 May 07 gregory 286          * Java.exe) until the garbage collector is run (and you don't
1652 22 May 07 gregory 287          * know when it will run) and detects that the file (the object
1652 22 May 07 gregory 288          * "coreInputStream") is no longer referenced from anywhere else
1652 22 May 07 gregory 289          * and it release the file by automatic. Until this happenes, no
1652 22 May 07 gregory 290          * other application can write to or rename this file (however,
1652 22 May 07 gregory 291          * this problem is solved with this line).
790 23 Oct 06 olle 292          */
790 23 Oct 06 olle 293         coreInputStream.close();
741 10 Oct 06 olle 294       }
1656 22 May 07 gregory 295     }
1656 22 May 07 gregory 296     catch (Exception e)
1656 22 May 07 gregory 297     {
741 10 Oct 06 olle 298       if (b_showErrors)
1656 22 May 07 gregory 299         System.out
1656 22 May 07 gregory 300           .println("An error occured @ writeFileToStream:\n" + e
1656 22 May 07 gregory 301             .getMessage());
741 10 Oct 06 olle 302     }
1651 21 May 07 olle 303     /*
1651 21 May 07 olle 304      * Close DbControl.
1651 21 May 07 olle 305      */
1651 21 May 07 olle 306     dc.close();
741 10 Oct 06 olle 307   }
741 10 Oct 06 olle 308
1656 22 May 07 gregory 309
2418 22 Nov 07 gregory 310   static public void writeASCIIFileToStream(File theFile, OutputStream os,
2418 22 Nov 07 gregory 311       SessionControl sc)
1656 22 May 07 gregory 312   {
1676 25 May 07 olle 313     writeFileToStream(theFile, os, 0, sc);
741 10 Oct 06 olle 314   }
741 10 Oct 06 olle 315
1656 22 May 07 gregory 316
1652 22 May 07 gregory 317   static public void writeFile(String s_theFile,
1676 25 May 07 olle 318       DataInputStreamWithReadLine bis, boolean b_append, SessionControl sc)
1656 22 May 07 gregory 319   {
790 23 Oct 06 olle 320     // *** Debug Info
1656 22 May 07 gregory 321     log
1656 22 May 07 gregory 322       .info("StreamIO::writeFile(): Start - s_theFile = \"" + s_theFile + "\" b_append = " + b_append);
790 23 Oct 06 olle 323     /*
790 23 Oct 06 olle 324      * Remove directory name from path to get pure filename
790 23 Oct 06 olle 325      */
1652 22 May 07 gregory 326     // String
1652 22 May 07 gregory 327     // s_theFileDir=s_theFile.substring(0,s_theFile.lastIndexOf(File.separatorChar)+1);
1652 22 May 07 gregory 328     // String
1652 22 May 07 gregory 329     // s_theFileName=s_theFile.substring(s_theFile.lastIndexOf(File.separatorChar)+1);
1652 22 May 07 gregory 330     String s_theFileName = s_theFile.substring(s_theFile
1656 22 May 07 gregory 331       .lastIndexOf(coreSeparatorChar) + 1);
1656 22 May 07 gregory 332     log
1656 22 May 07 gregory 333       .info("StreamIO::writeFile(): s_theFileName = \"" + s_theFileName + "\"");
1652 22 May 07 gregory 334     // String parentDirPath =
1652 22 May 07 gregory 335     // s_theFile.substring(0,s_theFile.lastIndexOf(File.separatorChar,
1652 22 May 07 gregory 336     // s_theFile.length()-2)+1);
1652 22 May 07 gregory 337     String parentDirPath = s_theFile.substring(0, s_theFile.lastIndexOf(
1656 22 May 07 gregory 338       coreSeparatorChar, s_theFile.length() - 2) + 1);
1656 22 May 07 gregory 339     log
1656 22 May 07 gregory 340       .info("StreamIO::writeFile(): parentDirPath = \"" + parentDirPath + "\"");
1652 22 May 07 gregory 341     // FileOutputStream fileStreamed=null;
790 23 Oct 06 olle 342     OutputStream coreOutputStream = null;
1652 22 May 07 gregory 343     // DbControl dc = FTPServer.getSessionControl().newDbControl();
1652 22 May 07 gregory 344     // int coreUserId = FTPServer.getSessionControl().getLoggedInUserId();
1652 22 May 07 gregory 345     // log.info ("StreamIO::writeFile(): coreUserId = " + coreUserId);
1652 22 May 07 gregory 346     // User coreUser = User.getById(dc, coreUserId);
1652 22 May 07 gregory 347     // log.info ("StreamIO::writeFile(): coreUser.getName() = \"" +
1652 22 May 07 gregory 348     // coreUser.getName() + "\"");
1652 22 May 07 gregory 349     // Directory homeDir = coreUser.getHomeDirectory();
1652 22 May 07 gregory 350     // log.info ("StreamIO::writeFile(): homeDir.getName() = \"" +
1652 22 May 07 gregory 351     // homeDir.getName() + "\"");
1652 22 May 07 gregory 352     // log.info ("StreamIO::writeFile(): dc.isClosed() = " + dc.isClosed());
1652 22 May 07 gregory 353     // log.info ("StreamIO::writeFile(): dc.isConnected() = " +
1652 22 May 07 gregory 354     // dc.isConnected());
790 23 Oct 06 olle 355     /*
1652 22 May 07 gregory 356      * In the future, coreDir may be any accessible sub-directory to the
1652 22 May 07 gregory 357      * homeDir of the logged-in user.
790 23 Oct 06 olle 358      */
1652 22 May 07 gregory 359     // Directory coreDir = homeDir;
1652 22 May 07 gregory 360     org.proteios.core.Path coreDirPath = new org.proteios.core.Path(
1656 22 May 07 gregory 361       parentDirPath, Type.DIRECTORY);
2418 22 Nov 07 gregory 362     // DbControl dc = FTPServer.getSessionControl().newDbControl();
1676 25 May 07 olle 363     DbControl dc = sc.newDbControl();
1656 22 May 07 gregory 364     ItemFactory factory = new ItemFactory(dc);
799 24 Oct 06 olle 365     Directory coreDir = null;
1656 22 May 07 gregory 366     try
1656 22 May 07 gregory 367     {
1656 22 May 07 gregory 368       coreDir = factory.getByPath(coreDirPath);
1656 22 May 07 gregory 369     }
1656 22 May 07 gregory 370     catch (Exception e)
1656 22 May 07 gregory 371     {
1652 22 May 07 gregory 372       log
1656 22 May 07 gregory 373         .info("StreamIO::writeFile(): Could not get parent core directory, return.");
799 24 Oct 06 olle 374       return;
799 24 Oct 06 olle 375     }
1656 22 May 07 gregory 376     log
1656 22 May 07 gregory 377       .info("StreamIO::writeFile(): coreDir.getPath().toString() = \"" + coreDir
1656 22 May 07 gregory 378         .getPath().toString() + "\"");
1656 22 May 07 gregory 379     org.proteios.core.File coreFile = factory
1656 22 May 07 gregory 380       .create(org.proteios.core.File.class);
1656 22 May 07 gregory 381     coreFile.setDirectory(coreDir);
1705 31 May 07 olle 382     coreFile.setProjectKey(coreDir.getProjectKey());
1656 22 May 07 gregory 383     log
1656 22 May 07 gregory 384       .info("StreamIO::writeFile(): (orig) coreFile.getName() = \"" + coreFile
1656 22 May 07 gregory 385         .getName() + "\"");
790 23 Oct 06 olle 386     coreFile.setName(s_theFileName);
1656 22 May 07 gregory 387     log
1656 22 May 07 gregory 388       .info("StreamIO::writeFile(): (updated) coreFile.getName() = \"" + coreFile
1656 22 May 07 gregory 389         .getName() + "\"");
3389 06 Aug 09 olle 390     // Try to get MIME type from file extension
3389 06 Aug 09 olle 391     String mimeTypeFromExtension = null;
3389 06 Aug 09 olle 392     if (s_theFileName != null && !s_theFileName.equals(""))
3389 06 Aug 09 olle 393     {
3389 06 Aug 09 olle 394       String filename = new String(s_theFileName);
3389 06 Aug 09 olle 395       int dotIndex = filename.lastIndexOf(".");
3389 06 Aug 09 olle 396       if (dotIndex > 0)
3389 06 Aug 09 olle 397       {
3389 06 Aug 09 olle 398         String fileExtension = filename.substring(dotIndex+1);
3389 06 Aug 09 olle 399         mimeTypeFromExtension = MimeType.getMimeTypeStringByExtension(dc, fileExtension);
3389 06 Aug 09 olle 400       }
3389 06 Aug 09 olle 401     }
3389 06 Aug 09 olle 402     if (mimeTypeFromExtension != null)
3389 06 Aug 09 olle 403     {
3389 06 Aug 09 olle 404       coreFile.setMimeType(mimeTypeFromExtension);
3389 06 Aug 09 olle 405       log
3389 06 Aug 09 olle 406         .info("StreamIO::writeFile(): (updated) coreFile.getMimeType() = \"" + coreFile
3389 06 Aug 09 olle 407           .getMimeType() + "\"");
3389 06 Aug 09 olle 408     }
790 23 Oct 06 olle 409     /*
790 23 Oct 06 olle 410      * Save core file item before getting uploadstream
790 23 Oct 06 olle 411      */
4197 25 Mar 11 olle 412     // Store file compressed internally as default
4194 25 Mar 11 fredrik 413     coreFile.setCompressed(true);
790 23 Oct 06 olle 414     dc.saveItem(coreFile);
1656 22 May 07 gregory 415     try
1656 22 May 07 gregory 416     {
4197 25 Mar 11 olle 417       boolean checkMd5Flag = false;
4197 25 Mar 11 olle 418       boolean compressFlag = true;
4197 25 Mar 11 olle 419       coreOutputStream = coreFile.getUploadStream(checkMd5Flag, compressFlag);
1656 22 May 07 gregory 420     }
1656 22 May 07 gregory 421     catch (Exception e)
1656 22 May 07 gregory 422     {
1652 22 May 07 gregory 423       log
1656 22 May 07 gregory 424         .info("StreamIO::writeFile(): Exception when trying to get upload stream for coreFile: " + e);
790 23 Oct 06 olle 425     }
1652 22 May 07 gregory 426     // InputStream coreInputStream = coreFile.getDownloadStream(0);
1656 22 May 07 gregory 427     if (coreOutputStream == null)
1656 22 May 07 gregory 428     {
1652 22 May 07 gregory 429       log.info("StreamIO::writeFile(): coreOutputStream == null");
1656 22 May 07 gregory 430     }
1656 22 May 07 gregory 431     else
1656 22 May 07 gregory 432     {
790 23 Oct 06 olle 433       log.info("StreamIO::writeFile(): coreOutputStream != null");
790 23 Oct 06 olle 434     }
1656 22 May 07 gregory 435     try
1656 22 May 07 gregory 436     {
1652 22 May 07 gregory 437       // setPriority(MIN_PRIORITY);
1652 22 May 07 gregory 438       int numberOfBytesThatWillBeSent = 0;
1652 22 May 07 gregory 439       // fileStreamed=new FileOutputStream(s_theFile, b_append);
1652 22 May 07 gregory 440       byte[] myBuffer = new byte[8192];// 40960]; //2048*20 (i tested
1656 22 May 07 gregory 441       // and this value seems to give
1656 22 May 07 gregory 442       // a very high speed when you
1656 22 May 07 gregory 443       // download files)
1656 22 May 07 gregory 444       while ((numberOfBytesThatWillBeSent = bis.read(myBuffer)) != -1)
1656 22 May 07 gregory 445       {
1656 22 May 07 gregory 446         log
1656 22 May 07 gregory 447           .info("StreamIO::writeFile(): myBuffer = \"" + myBuffer + "\"");
1652 22 May 07 gregory 448         // fileStreamed.write(myBuffer,0,numberOfBytesThatWillBeSent);
1652 22 May 07 gregory 449         coreOutputStream
1656 22 May 07 gregory 450           .write(myBuffer, 0, numberOfBytesThatWillBeSent);
741 10 Oct 06 olle 451       }
1652 22 May 07 gregory 452       // NO MORE TIMEOUT STUFF: yield();
1652 22 May 07 gregory 453       // NO MORE TIMEOUT STUFF: sleep(50); //Be safe, don't close before
1652 22 May 07 gregory 454       // all data has been sent
1652 22 May 07 gregory 455       // setPriority(NORM_PRIORITY);
1656 22 May 07 gregory 456     }
1656 22 May 07 gregory 457     catch (Exception e)
1656 22 May 07 gregory 458     {
741 10 Oct 06 olle 459       if (b_showErrors)
1656 22 May 07 gregory 460         System.out.println("An error occured @ writeFile:\n" + e
1656 22 May 07 gregory 461           .getMessage());
741 10 Oct 06 olle 462     }
1656 22 May 07 gregory 463     try
1656 22 May 07 gregory 464     {
1652 22 May 07 gregory 465       // if (fileStreamed!=null) //If fileStreamed "holds" a file
1652 22 May 07 gregory 466       // {
1652 22 May 07 gregory 467       // fileStreamed.flush();
790 23 Oct 06 olle 468       /*
1652 22 May 07 gregory 469        * Important: This must be reached no matter what! Even if an
1652 22 May 07 gregory 470        * exception occurs in the try block, this must be reached.
1652 22 May 07 gregory 471        * Otherwise the file will be locked by Xerver (locked by Java.exe)
1652 22 May 07 gregory 472        * until the garbage collector is runned (and you don't know when it
1652 22 May 07 gregory 473        * will run) and detects that the file (the object "fileStreamed")
1652 22 May 07 gregory 474        * is no longer referenced from anywhere else and it release the
1652 22 May 07 gregory 475        * file by automatic. Until this happenes, no other application can
1652 22 May 07 gregory 476        * write to or rename this file (however, this problem is solved
1652 22 May 07 gregory 477        * with this line).
790 23 Oct 06 olle 478        */
1652 22 May 07 gregory 479       // fileStreamed.close();
1652 22 May 07 gregory 480       // }
1652 22 May 07 gregory 481       if (coreOutputStream != null) // If coreOutputStream "holds" a
1656 22 May 07 gregory 482       // file
741 10 Oct 06 olle 483       {
790 23 Oct 06 olle 484         coreOutputStream.flush();
790 23 Oct 06 olle 485         /*
1652 22 May 07 gregory 486          * Important: This must be reached no matter what! Even if an
1652 22 May 07 gregory 487          * exception occurs in the try block, this must be reached.
1652 22 May 07 gregory 488          * Otherwise the file will be locked by Xerver (locked by
1652 22 May 07 gregory 489          * Java.exe) until the garbage collector is runned (and you
1652 22 May 07 gregory 490          * don't know when it will run) and detects that the file (the
1652 22 May 07 gregory 491          * object "fileStreamed") is no longer referenced from anywhere
1652 22 May 07 gregory 492          * else and it release the file by automatic. Until this
1652 22 May 07 gregory 493          * happenes, no other application can write to or rename this
1652 22 May 07 gregory 494          * file (however, this problem is solved with this line).
790 23 Oct 06 olle 495          */
790 23 Oct 06 olle 496         coreOutputStream.close();
790 23 Oct 06 olle 497         /*
790 23 Oct 06 olle 498          * Save downloaded core File item in database.
790 23 Oct 06 olle 499          */
790 23 Oct 06 olle 500         dc.saveItem(coreFile);
790 23 Oct 06 olle 501         dc.commit();
741 10 Oct 06 olle 502       }
1656 22 May 07 gregory 503     }
1656 22 May 07 gregory 504     catch (Exception e)
1656 22 May 07 gregory 505     {
741 10 Oct 06 olle 506       if (b_showErrors)
1656 22 May 07 gregory 507         System.out.println("An error occured @ writeFile:\n" + e
1656 22 May 07 gregory 508           .getMessage());
741 10 Oct 06 olle 509     }
1651 21 May 07 olle 510     /*
4197 25 Mar 11 olle 511      * Check if file compression is motivated.
4197 25 Mar 11 olle 512      */
4197 25 Mar 11 olle 513     dc = sc.newDbControl();
4197 25 Mar 11 olle 514     factory.setDc(dc);
4197 25 Mar 11 olle 515     int coreFileId = coreFile.getId();
4197 25 Mar 11 olle 516     // Get new coreFile item from file id using the new DbControl
4197 25 Mar 11 olle 517     coreFile = factory.getById(org.proteios.core.File.class, coreFileId);
4197 25 Mar 11 olle 518     long coreFileSizeInBytes = coreFile.getSizeInBytes();
4197 25 Mar 11 olle 519     long coreFileCompressedSizeInBytes = coreFile.getCompressedSizeInBytes();
4197 25 Mar 11 olle 520     float compressionFraction = (float) 1.0;
4197 25 Mar 11 olle 521     if (coreFileSizeInBytes > 0)
4197 25 Mar 11 olle 522     {
4197 25 Mar 11 olle 523       compressionFraction = ((float) coreFileCompressedSizeInBytes)/((float) coreFileSizeInBytes);
4197 25 Mar 11 olle 524     }
4197 25 Mar 11 olle 525     log.info("StreamIO::writeFile(): coreFile \"" + coreFile.getName() + "\" coreFileSizeInBytes = " + coreFileSizeInBytes + " coreFileCompressedSizeInBytes = " + coreFileCompressedSizeInBytes + " compressionFraction = " + compressionFraction);
4197 25 Mar 11 olle 526     if (compressionFraction > 0.80)
4197 25 Mar 11 olle 527     {
4197 25 Mar 11 olle 528       // Store file uncompressed internally
4197 25 Mar 11 olle 529       if (coreFile.isCompressed())
4197 25 Mar 11 olle 530       {
4197 25 Mar 11 olle 531         coreFile.decompress(null);
4197 25 Mar 11 olle 532         coreFile.setCompressed(false);
4197 25 Mar 11 olle 533       }
4197 25 Mar 11 olle 534     }
4197 25 Mar 11 olle 535     try
4197 25 Mar 11 olle 536     {
4197 25 Mar 11 olle 537       dc.commit();
4197 25 Mar 11 olle 538     }
4197 25 Mar 11 olle 539     catch (Exception e)
4197 25 Mar 11 olle 540     {
4197 25 Mar 11 olle 541       if (b_showErrors)
4197 25 Mar 11 olle 542         System.out.println("An error occured @ writeFile:\n" + e
4197 25 Mar 11 olle 543           .getMessage());
4197 25 Mar 11 olle 544     }
4197 25 Mar 11 olle 545     /*
1651 21 May 07 olle 546      * Close DbControl.
1651 21 May 07 olle 547      */
1651 21 May 07 olle 548     dc.close();
741 10 Oct 06 olle 549   }
790 23 Oct 06 olle 550
1656 22 May 07 gregory 551
790 23 Oct 06 olle 552   /**
1652 22 May 07 gregory 553    * Convenience method, not part of the original Xerver code. Based on method
1652 22 May 07 gregory 554    * makeSlashAtEnd(String path) in class FTPNewConnection, but always uses a
1652 22 May 07 gregory 555    * slash "/" instead of File.separator, as the former is the standard used
1652 22 May 07 gregory 556    * for core files and directories.
1652 22 May 07 gregory 557    * 
1656 22 May 07 gregory 558    * @param path String with path
1652 22 May 07 gregory 559    * @return String path ending with slash
1652 22 May 07 gregory 560    */
1656 22 May 07 gregory 561   private static String makeSlashAtEnd(String path)
1656 22 May 07 gregory 562   {
1052 08 Dec 06 olle 563     if (path.endsWith(coreSeparator))
790 23 Oct 06 olle 564       return path;
2418 22 Nov 07 gregory 565     return path + coreSeparator;
790 23 Oct 06 olle 566   }
741 10 Oct 06 olle 567 }