org/objectweb/proactive/core/filetransfer/FileBlock.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.filetransfer;
00032 
00033 /*
00034  * ################################################################
00035  *
00036  * ProActive: The Java(TM) library for Parallel, Distributed, Concurrent
00037  * computing with Security and Mobility
00038  *
00039  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis Contact:
00040  * proactive-support@inria.fr
00041  *
00042  * This library is free software; you can redistribute it and/or modify it under
00043  * the terms of the GNU Lesser General Public License as published by the Free
00044  * Software Foundation; either version 2.1 of the License, or any later version.
00045  *
00046  * This library is distributed in the hope that it will be useful, but WITHOUT
00047  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00048  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
00049  * details.
00050  *
00051  * You should have received a copy of the GNU Lesser General Public License
00052  * along with this library; if not, write to the Free Software Foundation, Inc.,
00053  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00054  *
00055  * Initial developer(s): The ProActive Team
00056  * http://www.inria.fr/oasis/ProActive/contacts.html Contributor(s):
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; //Bytes
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 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1