001 /** 002 * Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of version 2 of the GNU General Public License as 005 * published by the Free Software Foundation. 006 * There are special exceptions to the terms and conditions of the GPL 007 * as it is applied to this software. See the GNU General Public License for more details. 008 * 009 * This program 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 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 017 */ 018 019 package org.dbreplicator.replication.column; 020 021 import java.io.*; 022 import java.io.File; 023 024 import org.dbreplicator.replication.*; 025 import org.apache.log4j.Logger; 026 027 /** 028 * 029 */ 030 public class BlobOutPutStream { 031 public int streamEnd; 032 private String path = null; // = PathHandler.getLobFilePathForServer(); 033 protected static Logger log =Logger.getLogger(BlobOutPutStream.class.getName()); 034 035 public BlobOutPutStream(String path0) { 036 path = path0; 037 } 038 039 public int write(InputStream input) { 040 int bytesToWrite = 0; 041 /* try 042 { 043 bytesToWrite = input.available(); 044 } 045 catch (IOException ex2) 046 { 047 // ex2.printStackTrace(); 048 } 049 if (bytesToWrite == 0 || bytesToWrite == -1) 050 { 051 return 0; 052 } */ 053 int len = 0; 054 int length = 0; 055 byte[] buf = new byte[1024]; 056 try { 057 FileOutputStream output = new FileOutputStream(path, true); 058 while ( (len = input.read(buf)) > 0) { 059 output.write(buf, 0, len); 060 //lastIndex += len; 061 length += len; 062 bytesToWrite += len; 063 } 064 output.flush(); 065 output.close(); 066 // streamEnd = length; 067 return bytesToWrite; 068 } 069 catch (IOException ex) { 070 RepConstants.writeERROR_FILE(ex); 071 return -1; 072 } 073 } 074 075 public InputStream getInputStream() { 076 try { 077 File file = new File(path); 078 if (!file.exists()) { 079 // file = new File(path); 080 try { 081 file.createNewFile(); 082 } 083 catch (IOException ex) { 084 log.error(ex.getMessage(),ex); 085 } 086 } 087 return new FileInputStream(path); 088 } 089 catch (FileNotFoundException ex) { 090 RepConstants.writeERROR_FILE(ex); 091 return null; 092 } 093 } 094 095 public int getStreamStart() { 096 InputStream is = null; 097 int streamStart = 0; 098 try { 099 is = getInputStream(); 100 streamStart = is.available(); 101 } 102 catch (IOException ex1) { 103 log.error(ex1.getMessage(),ex1); 104 // Problem with Input 105 } 106 finally { 107 try { 108 if (is != null) { 109 is.close(); 110 } 111 } 112 catch (IOException ex) { 113 log.error(ex.getMessage(),ex); 114 // ex.printStackTrace(); 115 } 116 } 117 return streamStart; 118 } 119 120 public String toString() { 121 return "[BlobOutStream end [" + streamEnd + "]]"; 122 } 123 }

