001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.objectmanager.cache; 031 032import java.io.BufferedInputStream; 033import java.io.BufferedOutputStream; 034import java.io.File; 035import java.io.FileInputStream; 036import java.io.FileNotFoundException; 037import java.io.FileOutputStream; 038import java.io.IOException; 039 040import org.apache.commons.logging.Log; 041import org.apache.commons.logging.LogFactory; 042 043public abstract class BufferedDataCacheObject extends DataCacheObject { 044 045 private static Log log; 046 047 static { 048 log = LogFactory 049 .getLog("org.kepler.objectmanager.cache.BufferedDataCacheObject"); 050 } 051 052 /** 053 * Sets the data into the cache item 054 * 055 *@param aData 056 * the new data byte array 057 */ 058 public void setData(byte[] aData) { 059 saveData(aData); 060 } 061 062 /** 063 * Returns the data as a byte array 064 * 065 * */ 066 public byte[] getData() throws CacheException { 067 return loadData(); 068 } 069 070 /** 071 * Loads the data from the cache (a file) 072 * 073 *@return whether it was able to load the data 074 */ 075 private byte[] loadData() throws CacheException { 076 log.debug("getName() = " + getName()); 077 log.debug("getResourceName() = " + getResourceName()); 078 log.debug("getLocalFileName() = " + getAbsoluteFileName()); 079 File file = getFile(); 080 081 if (file == null || !file.exists()) { 082 log.debug("File is null or does not exist"); 083 throw new CacheException("File for " + getName() 084 + " is null or does not exist"); 085 } 086 087 long size = file.length(); 088 089 if (size > Integer.MAX_VALUE) { 090 // XXX this could be bad for large datasets 091 log.debug("loadData - Too Much Data for byte array:"); 092 throw new CacheException("Too much data for byte array"); 093 } 094 if (size <= 0) { 095 log.debug("loadData - File empty:"); 096 return new byte[0]; 097 } 098 099 int iSize = (int) size; 100 101 try { 102 byte[] mData = new byte[iSize]; 103 104 FileInputStream fis = new FileInputStream(file); 105 106 BufferedInputStream bis = new BufferedInputStream(fis); 107 108 int retSize = bis.read(mData, 0, iSize); 109 110 if (retSize != iSize) { 111 log.debug("loadData - Wrong amount of data read:"); 112 throw new CacheException("Wrong amount of data read"); 113 } 114 115 bis.close(); 116 log.debug("DataCacheObject - Data was loaded:"); 117 return mData; 118 } catch (FileNotFoundException e) { 119 throw new CacheException("File for " + getName() 120 + " does not exist"); 121 } catch (IOException e) { 122 throw new CacheException("File for " + getName() + " io exception"); 123 } 124 } 125 126 /** 127 * Save the data out to a file 128 * 129 * */ 130 private boolean saveData(byte[] mData) { 131 if (mData != null) { 132 try { 133 if (mData.length > 0) { 134 File file = getFile(); 135 FileOutputStream fos = new FileOutputStream(file); 136 BufferedOutputStream bos = new BufferedOutputStream(fos); 137 138 bos.write(mData); 139 bos.close(); 140 } 141 142 } catch (Exception e) { 143 System.err.println(e); 144 return false; 145 } 146 } 147 return true; 148 } 149 150}