JavaDoc


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.sql.*;
023    
024    import org.dbreplicator.replication.*;
025    import org.apache.log4j.Logger;
026    
027    /**
028     *
029     */
030    public class RClob implements Clob
031    {
032    
033        private InputStream is;
034        private long start;
035        private long length;
036        protected static Logger log =Logger.getLogger(RClob.class.getName());
037    
038       public RClob(long start0, long length0) {
039            start = start0;
040            length = length0;
041          try {
042                String path = PathHandler.getCLobFilePathForClient();
043    // RepPrinter.print(" Inside RClob path for file " + path);
044                is = new FileInputStream(path);
045             is.skip(start);
046          } catch (Exception ex) {
047              log.error(ex.getMessage(),ex);
048                //Ignore Exception.
049            }
050        }
051    
052       public long length() throws SQLException {
053            return length;
054    
055    //    try {
056    //      return (long) getAsciiStream().available();
057    //    }
058    //    catch (SQLException ex) {
059    //      return 0L;
060    //    }
061    //    catch (IOException ex) {
062    //      return 0L;
063    //    }
064        }
065    
066       /*   public String getSubString(long pos, int length0) throws SQLException {
067    //       RepPrinter.print(" Inside RClob start : " + start + " length : " + length);
068             if (length0 == 0) {
069                return "";
070            }
071             if (pos != 0) {
072                pos--;
073            }
074            long start1 = start + pos;
075            length0 = (start1 + length0) < (start + length) ?
076                length0 : (int) (length - pos);
077    //       RepPrinter.print(" Inside RClob start1 : " + start1 + " length0 : " +length0);
078            byte[] buf = new byte[length0];
079            int lengthRead = 0;
080            long skip = -1;
081             try {
082                if (start1 != 0) {
083                    skip = is.skip(start1);
084                }
085                lengthRead = is.read(buf, 0, length0);
086             } catch (IOException ex) {
087                throw new SQLException(ex.getMessage());
088             }
089             return new String(buf);
090          }*/
091    
092       public String getSubString(long pos, int length0) throws SQLException {
093          if (length0 <= 0) {
094             return "";
095          }
096    
097          if (pos != 0) {pos--;
098          }
099    
100          long start1 = start + pos;
101          length0 = (start1 + length0) < (start + length) ?
102              length0 : (int) (length - pos);
103    //    RepPrinter.print(" Inside RClob start1 : " + start1 + " length0 : " +length0);
104          byte[] buf = new byte[length0];
105          int lengthRead = 0;
106          try {
107            lengthRead = is.read(buf, 0, length0);
108            if (lengthRead <= 0) {
109              throw new SQLException("NODATA");
110            }
111          }
112            catch (IOException ex)
113            {
114              log.error(ex.getMessage(),ex);
115                throw new SQLException(ex.getMessage());
116            }
117    
118          byte[] temp = new byte[lengthRead];
119          System.arraycopy(buf, 0, temp, 0, lengthRead);
120          return new String(temp);
121        }
122    
123        public Reader getCharacterStream() throws SQLException
124        {
125            return new InputStreamReader(getAsciiStream());
126        }
127    
128    // bjt
129        public Reader getCharacterStream(long arg0, long arg1) throws SQLException
130        {
131            /**@todo Implement this java.sql.Clob method*/
132            throw new java.lang.UnsupportedOperationException(
133                "Method getCharacterStream(long,long) not yet implemented.");
134        }
135        public void free() throws SQLException {
136            /**@todo Implement this java.sql.Clob method*/
137            throw new java.lang.UnsupportedOperationException(
138                "Method free() not yet implemented.");
139        }
140    // end bjt
141    
142       public InputStream getAsciiStream() throws SQLException {
143    //    return new ByteArrayInputStream(getSubString(0, (int) length).getBytes());
144          return new RepAsciiStream(this);
145        }
146    
147        public long position(String searchstr, long start) throws SQLException
148        {
149            /**@todo Implement this java.sql.Clob method*/
150            throw new java.lang.UnsupportedOperationException(
151                "Method position() not yet implemented.");
152        }
153    
154       public long position(Clob searchstr, long start) throws SQLException {
155            /**@todo Implement this java.sql.Clob method*/
156            throw new java.lang.UnsupportedOperationException(
157                "Method position() not yet implemented.");
158        }
159    
160        public int setString(long pos, String str) throws SQLException
161        {
162            /**@todo Implement this java.sql.Clob method*/
163            throw new java.lang.UnsupportedOperationException(
164                "Method setString() not yet implemented.");
165        }
166    
167        public int setString(long pos, String str, int offset, int len) throws
168            SQLException
169        {
170            /**@todo Implement this java.sql.Clob method*/
171            throw new java.lang.UnsupportedOperationException(
172                "Method setString() not yet implemented.");
173        }
174    
175        public OutputStream setAsciiStream(long pos) throws SQLException
176        {
177            /**@todo Implement this java.sql.Clob method*/
178            throw new java.lang.UnsupportedOperationException(
179                "Method setAsciiStream() not yet implemented.");
180        }
181    
182        public Writer setCharacterStream(long pos) throws SQLException
183        {
184            /**@todo Implement this java.sql.Clob method*/
185            throw new java.lang.UnsupportedOperationException(
186                "Method setCharacterStream() not yet implemented.");
187        }
188    
189        public void truncate(long len) throws SQLException
190        {
191            /**@todo Implement this java.sql.Clob method*/
192            throw new java.lang.UnsupportedOperationException(
193                "Method truncate() not yet implemented.");
194        }
195    
196        public boolean equals(RClob obj) throws Exception
197        {
198            InputStream currentStream = this.getAsciiStream();
199            InputStream incommingStream = obj.getAsciiStream();
200            int currentByte = currentStream.read();
201            int incommingByte = incommingStream.read();
202          while (currentByte != -1) {
203             if (incommingByte != -1) {
204                if (currentByte != incommingByte) {
205                        return false;
206                    }
207                    currentByte = currentStream.read();
208                    incommingByte = incommingStream.read();
209                }
210                else
211                {
212                    return false;
213                }
214            }
215            if (incommingByte != -1)
216            {
217                return false;
218            }
219            return true;
220        }
221    
222        public boolean equals1(RClob obj) throws Exception
223        {
224            InputStream currentStream = this.getAsciiStream();
225            InputStream incommingStream = obj.getAsciiStream();
226            long len = length;
227            do
228            {
229                byte[] buf1 = new byte[1024];
230                byte[] buf2 = new byte[1024];
231                int currentByte = currentStream.read(buf1);
232                int incommingByte = incommingStream.read(buf2);
233                if (currentByte != incommingByte)
234                {
235                    return false;
236                }
237                for (int i = 0; i < buf1.length; i++)
238                {
239                    if (buf1[i] != buf2[i])
240                    {
241                        return false;
242                    }
243                }
244                len -= currentByte;
245            }
246            while (len > 0);
247            return true;
248        }
249    }





























































Powered by Drupal - Theme by Danger4k