1 21 22 package org.apache.derby.impl.load; 23 24 import java.io.FileOutputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.Date ; 30 import java.io.IOException ; 31 32 36 final class ExportWriteData extends ExportWriteDataAbstract 37 implements java.security.PrivilegedExceptionAction { 38 39 private String outputFileName; 40 private OutputStreamWriter aStream; 43 44 ExportWriteData(String outputFileName, ControlInfo controlFileReader) 46 throws Exception { 47 this.outputFileName = outputFileName; 48 this.controlFileReader = controlFileReader; 49 loadPropertiesInfo(); 50 51 try { 52 java.security.AccessController.doPrivileged(this); 53 } catch (java.security.PrivilegedActionException pae) { 54 throw pae.getException(); 55 } 56 57 } 58 59 public final Object run() throws Exception { 60 openFile(); 61 return null; 62 } 63 64 private void openFile() throws Exception { 66 try { 67 URL url = new URL (outputFileName); 68 outputFileName = url.getFile(); 69 } catch (MalformedURLException ex) {} 70 FileOutputStream anOutputStream = new FileOutputStream (outputFileName); 71 BufferedOutputStream buffered = new BufferedOutputStream (anOutputStream); 72 73 aStream = dataCodeset == null ? 74 new OutputStreamWriter (buffered) : 75 new OutputStreamWriter (buffered, dataCodeset); 76 } 77 78 82 void writeColumnDefinitionOptionally(String [] columnNames, 83 String [] columnTypes) 84 throws Exception { 85 boolean ignoreColumnTypes=true; 86 87 if (columnDefinition.toUpperCase(java.util.Locale.ENGLISH).equals(ControlInfo.INTERNAL_TRUE.toUpperCase(java.util.Locale.ENGLISH))) { 89 String tempStr=new String (); 90 for (int i=0; i<columnNames.length; i++) { 92 if (i>0) { 95 tempStr=fieldSeparator; 96 } else { 97 tempStr=""; 98 } 99 100 tempStr=tempStr+ 101 fieldStartDelimiter+columnNames[i]+fieldStopDelimiter; 102 if (ignoreColumnTypes==false) { 103 tempStr=tempStr+fieldSeparator+ 104 fieldStartDelimiter+columnTypes[i]+fieldStopDelimiter; 105 } 106 107 aStream.write(tempStr, 0, tempStr.length()); 108 } 109 aStream.write(recordSeparator, 0, recordSeparator.length()); 110 } 111 } 112 113 private void writeNextColumn(String oneColumn, boolean isNumeric) throws Exception { 116 if (oneColumn != null) { 117 if (!isNumeric) 124 aStream.write(fieldStartDelimiter, 0, fieldStartDelimiter.length()); 125 if(doubleDelimiter) 127 oneColumn = makeDoubleDelimiterString(oneColumn , fieldStartDelimiter); 128 aStream.write(oneColumn, 0, oneColumn.length()); 129 if (!isNumeric) 130 aStream.write(fieldStopDelimiter, 0, fieldStopDelimiter.length()); 131 } 132 } 133 134 137 public void writeData(String [] oneRow, boolean[] isNumeric) throws Exception { 138 if (format.equals(ControlInfo.DEFAULT_FORMAT)) { 139 writeNextColumn(oneRow[0], isNumeric[0]); 142 for (int i = 1; i < oneRow.length; i++) { 143 aStream.write(fieldSeparator, 0, fieldSeparator.length()); 144 writeNextColumn(oneRow[i], isNumeric[i]); 145 } 146 if (hasDelimiterAtEnd){ aStream.write(fieldSeparator, 0, fieldSeparator.length()); 148 } 149 } 150 aStream.write(recordSeparator, 0, recordSeparator.length()); 151 } 152 153 157 public void noMoreRows() throws IOException { 158 aStream.flush(); 159 aStream.close(); 160 } 164 165 166 190 private String makeDoubleDelimiterString(String inputString , String charDelimiter) 191 { 192 int start = inputString.indexOf(charDelimiter); 193 StringBuffer result; 194 if(start != -1) 196 { 197 result = new StringBuffer (inputString); 198 int current; 199 int delLength = charDelimiter.length(); 200 while(start!= -1) 201 { 202 result = result.insert(start, charDelimiter); 204 current = start + delLength +1 ; 205 start = result.toString().indexOf(charDelimiter, current); 206 } 207 return result.toString(); 208 } 209 return inputString; 210 } 211 } 212 213 | Popular Tags |