001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: FileTransport.java$
021: * $FileID: 4199$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:04 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: */
028:
029: package org.sourcejammer.client;
030:
031: import java.io.*;
032:
033: import org.sourcejammer.util.ProcessTimer;
034: import org.sourcejammer.util.SourceJammerConnectionException;
035: import org.sourcejammer.project.view.DownloadFileIdentifier;
036: import java.net.MalformedURLException;
037:
038: /**
039: * Title: $FileName: FileTransport.java$
040: * @author $AuthorName: Rob MacGrogan$
041: * @version $$VerNum: 2$
042: *
043: * $description: $
044: * $KeyWordsOff: $<br><br>
045: */
046: public class FileTransport {
047:
048: private FileTransport() {
049: }
050:
051: public static long sendFullFileToServer(File fl, String url)
052: throws IOException, SourceJammerConnectionException,
053: MalformedURLException {
054: long lReturn = -1;
055: if (!fl.exists()) {
056: throw new IOException("File does not exist.");
057: }
058: if (fl.isDirectory()) {
059: throw new IOException("Cannot send file. It's a directory.");
060: }
061: UploaderProxy uploader = new UploaderProxy(url);
062: lReturn = uploader.uploadFile(fl);
063: return lReturn;
064: }
065:
066: public static long sendFileToServer(File fl, String url)
067: throws IOException, SourceJammerConnectionException,
068: MalformedURLException {
069: long lReturn = -1;
070: if (!fl.exists()) {
071: throw new IOException("File does not exist.");
072: }
073: if (fl.isDirectory()) {
074: throw new IOException("Cannot send file. It's a directory.");
075: }
076:
077: UploaderProxy uploader = new UploaderProxy(url);
078:
079: FileInputStream stmIn = new FileInputStream(fl);
080: int iMaxChunk = SourceJammerClient.getInstance()
081: .getMaxFileSendChunkSize();
082:
083: int iMaxBufferSize = 100;
084: byte[] buffer = null;
085:
086: boolean bKeepSending = true;
087: while (bKeepSending) {
088: ByteArrayOutputStream oByOut = new ByteArrayOutputStream();
089: boolean bKeepReading = true;
090: int iByteCounter = 0;
091: while (bKeepReading) {
092: int iBufferSize = iMaxBufferSize;
093: if (iMaxChunk - iByteCounter < iMaxBufferSize) {
094: iBufferSize = iMaxChunk - iByteCounter;
095: }
096: buffer = new byte[iBufferSize];
097: int iNumRead = stmIn.read(buffer);
098: if (iNumRead == -1) {
099: bKeepSending = false;
100: bKeepReading = false;
101: } else {
102: if (iNumRead < iBufferSize) {
103: bKeepSending = false;
104: bKeepReading = false;
105: }
106: oByOut.write(buffer, 0, iNumRead);
107: iByteCounter += iNumRead;
108: if (iByteCounter >= iMaxChunk) {
109: bKeepReading = false;
110: }
111: }//end else num read not -1
112: }//end while keep reading
113: byte[] sendBytes = oByOut.toByteArray();
114: uploader.uploadBytes(sendBytes, !bKeepSending);
115: }//end while keep sending
116:
117: lReturn = uploader.getFileUploadID();
118: return lReturn;
119: }
120:
121: public static void downloadFullFileFromServer(
122: DownloadFileIdentifier id, File destination, String url)
123: throws MalformedURLException,
124: SourceJammerConnectionException, IOException {
125: DownloaderProxy downloader = new DownloaderProxy(url);
126: FileOutputStream stmOut = new FileOutputStream(destination);
127: try {
128: downloader.downloadFile(id, stmOut);
129: } finally {
130: stmOut.close();
131: }
132: }
133:
134: /**
135: * Streams file to OutputStream.
136: */
137: public static void downloadFullFileToStream(
138: DownloadFileIdentifier id, OutputStream stmOut, String url)
139: throws MalformedURLException,
140: SourceJammerConnectionException, IOException {
141: DownloaderProxy downloader = new DownloaderProxy(url);
142: downloader.downloadFile(id, stmOut);
143: }
144:
145: public static void downloadFileFromServer(
146: DownloadFileIdentifier id, File destination, String url)
147: throws MalformedURLException,
148: SourceJammerConnectionException, IOException {
149: int iMaxChunk = SourceJammerClient.getInstance()
150: .getMaxFileSendChunkSize();
151:
152: DownloaderProxy downloader = new DownloaderProxy(url);
153: downloader.setFileIdentifier(id);
154:
155: FileOutputStream stmOut = new FileOutputStream(destination);
156: try {
157: while (!downloader.endOfFile()) {
158: downloader.downloadBytes(iMaxChunk, stmOut);
159: stmOut.flush();
160: }
161: } finally {
162: stmOut.close();
163: }
164: }
165: }
|