00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.filetransfer;
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 import java.io.BufferedInputStream;
00061 import java.io.BufferedOutputStream;
00062 import java.io.IOException;
00063 import java.io.Serializable;
00064 
00065 import org.apache.log4j.Logger;
00066 import org.objectweb.proactive.core.util.log.Loggers;
00067 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00068 
00069 
00078 public class FileBlock implements Serializable {
00079     protected static Logger logger = ProActiveLogger.getLogger(Loggers.FILETRANSFER);
00080     public static final int DEFAULT_BLOCK_SIZE = 256 * 1024; 
00081     private byte[] buffer;
00082     private int usage;
00083     private long offset;
00084     private int blockSize;
00085     private Exception exception;
00086 
00090     public FileBlock() {
00091     }
00092 
00093     public FileBlock(long offset) {
00094         this(offset, DEFAULT_BLOCK_SIZE);
00095     }
00096 
00097     public FileBlock(long offset, int blockSize) {
00098         this.offset = offset;
00099         this.blockSize = blockSize;
00100         this.buffer = new byte[blockSize];
00101 
00102         this.exception = null;
00103         this.usage = 0;
00104     }
00105 
00112     public void loadNextBlock(BufferedInputStream bis)
00113         throws IOException {
00114         if (bis == null) {
00115             throw new IllegalArgumentException(
00116                 "Can not handle null BufferInputStream parameter.");
00117         }
00118 
00119         try {
00120             usage = bis.read(buffer, 0, blockSize);
00121             offset += usage;
00122         } catch (IOException e) {
00123             usage = 0;
00124             throw e;
00125         }
00126     }
00127 
00133     public void saveCurrentBlock(BufferedOutputStream bos)
00134         throws IOException {
00135         if (bos == null) {
00136             throw new IllegalArgumentException(
00137                 "Can not handle null BufferedOutputStream parameter.");
00138         }
00139 
00140         bos.write(buffer, 0, usage);
00141     }
00142 
00146     public long getOffset() {
00147         return offset;
00148     }
00149 
00153     public int getBlockSize() {
00154         return blockSize;
00155     }
00156 
00157     public Exception getException() {
00158         return exception;
00159     }
00160 
00161     public boolean hasException() {
00162         return exception != null;
00163     }
00164 
00165     public void setException(Exception e) {
00166         exception = e;
00167     }
00168 }