001package com.hfg.util.io; 002 003import java.io.File; 004import java.io.InputStream; 005import java.io.IOException; 006import java.io.BufferedOutputStream; 007import java.io.FileOutputStream; 008import java.io.OutputStream; 009import java.util.Calendar; 010 011 012//------------------------------------------------------------------------------ 013/** 014 * Abstract implementation of RemoteFile. 015 * 016 * @author J. Alex Taylor, hairyfatguy.com 017 */ 018//------------------------------------------------------------------------------ 019// com.hfg XML/HTML Coding Library 020// 021// This library is free software; you can redistribute it and/or 022// modify it under the terms of the GNU Lesser General Public 023// License as published by the Free Software Foundation; either 024// version 2.1 of the License, or (at your option) any later version. 025// 026// This library is distributed in the hope that it will be useful, 027// but WITHOUT ANY WARRANTY; without even the implied warranty of 028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 029// Lesser General Public License for more details. 030// 031// You should have received a copy of the GNU Lesser General Public 032// License along with this library; if not, write to the Free Software 033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 034// 035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 036// jataylor@hairyfatguy.com 037//------------------------------------------------------------------------------ 038 039 040public abstract class AbstractRemoteFile implements RemoteFile 041{ 042 //########################################################################### 043 // CONSTRUCTORS 044 //########################################################################### 045 046 //--------------------------------------------------------------------------- 047 public AbstractRemoteFile() 048 { 049 } 050 051 052 //########################################################################### 053 // PUBLIC METHODS 054 //########################################################################### 055 056 //--------------------------------------------------------------------------- 057 @Override 058 public String toString() 059 { 060/* 061 StringBuilder buffer = new StringBuilder(); 062 buffer.append("\nNAME: " + getName() + "\n"); 063 buffer.append("PATH: " + getPath() + "\n"); 064 buffer.append("SIZE: " + getSize() + "\n"); 065 buffer.append("TIMESTAMP: " + getTimestamp().getTime() + "\n"); 066 067 return buffer.toString(); 068*/ 069 return getURL(); 070 } 071 072 //--------------------------------------------------------------------------- 073 public boolean writeToLocalDir(File inLocalDir) 074 throws IOException 075 { 076 return writeToLocalFile(new File(inLocalDir, getName())); 077 } 078 079 //--------------------------------------------------------------------------- 080 public boolean copyLocallyPreservingPath(File inLocalDir) 081 throws IOException 082 { 083 return writeToLocalFile(new File(inLocalDir, getRequestedPath())); 084 } 085 086 //--------------------------------------------------------------------------- 087 public long writeToStream(OutputStream stream) 088 throws IOException 089 { 090 InputStream inputStream = getInputStream(); 091 byte buffer[] = new byte[8 * 1024]; 092 long fileSize = 0; 093 int bytesRead; 094 while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) 095 { 096 stream.write(buffer, 0, bytesRead); 097 fileSize += bytesRead; 098 } 099 100 inputStream.close(); 101 102 return fileSize; 103 } 104 105 //--------------------------------------------------------------------------- 106 public boolean writeToLocalFile(File inLocalFile) 107 throws IOException 108 { 109 File localDir = inLocalFile.getParentFile(); 110 if (localDir != null 111 && ! localDir.exists()) 112 { 113 if (!localDir.mkdirs()) 114 { 115 throw new RuntimeException("Couldn't make the dir '" + localDir + "'!"); 116 } 117 } 118 119 if (inLocalFile.exists() 120 && inLocalFile.length() == getSize()) 121 { 122 Calendar timestamp = getTimestamp(); 123 if (timestamp != null 124 && inLocalFile.lastModified() >= timestamp.getTimeInMillis()) 125 { 126 // The local file appears to be up to date. 127 System.out.println("Local file is up-to-date."); 128 return false; 129 } 130 } 131 132 BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(inLocalFile)); 133 writeToStream(outStream); 134 outStream.close(); 135 136 // Attempt to preserve the file's last modified timestamp 137 Calendar timestamp = getTimestamp(); 138 if (timestamp != null 139 && timestamp.getTimeInMillis() > 0) 140 { 141 inLocalFile.setLastModified(timestamp.getTimeInMillis()); 142 } 143 144 return true; 145 } 146}