1 22 23 package org.gjt.sp.jedit.bufferio; 24 25 import javax.swing.text.Segment ; 27 import java.io.*; 28 import java.nio.charset.*; 29 import java.nio.CharBuffer ; 30 import java.nio.ByteBuffer ; 31 import org.gjt.sp.jedit.io.*; 32 import org.gjt.sp.jedit.*; 33 import org.gjt.sp.jedit.buffer.JEditBuffer; 34 import org.gjt.sp.util.*; 35 37 42 public abstract class BufferIORequest extends WorkRequest 43 { 44 public static final int UTF8_MAGIC_1 = 0xef; 46 public static final int UTF8_MAGIC_2 = 0xbb; 47 public static final int UTF8_MAGIC_3 = 0xbf; 48 49 52 public static final int GZIP_MAGIC_1 = 0x1f; 53 public static final int GZIP_MAGIC_2 = 0x8b; 54 public static final int UNICODE_MAGIC_1 = 0xfe; 55 public static final int UNICODE_MAGIC_2 = 0xff; 56 57 61 public static final int XML_PI_LENGTH = 50; 62 63 66 public static final int IOBUFSIZE = 32768; 67 68 71 public static final int PROGRESS_INTERVAL = 300; 72 73 public static final String LOAD_DATA = "BufferIORequest__loadData"; 74 public static final String END_OFFSETS = "BufferIORequest__endOffsets"; 75 public static final String NEW_PATH = "BufferIORequest__newPath"; 76 77 80 public static final String ERROR_OCCURRED = "BufferIORequest__error"; 81 82 84 93 protected BufferIORequest(View view, Buffer buffer, 94 Object session, VFS vfs, String path) 95 { 96 this.view = view; 97 this.buffer = buffer; 98 this.session = session; 99 this.vfs = vfs; 100 this.path = path; 101 102 markersPath = vfs.getParentOfPath(path) 103 + '.' + vfs.getFileName(path) 104 + ".marks"; 105 } 107 public String toString() 109 { 110 return getClass().getName() + '[' + buffer + ']'; 111 } 113 115 protected final View view; 117 protected final Buffer buffer; 118 protected final Object session; 119 protected final VFS vfs; 120 protected String path; 121 protected final String markersPath; 122 124 129 protected Reader autodetect(InputStream in) throws IOException 130 { 131 return MiscUtilities.autodetect(in, buffer); 132 } 134 protected SegmentBuffer read(Reader in, long length, 136 boolean insert) throws IOException 137 { 138 139 IntegerArray endOffsets = new IntegerArray( 140 Math.max(1,(int)(length / 50))); 141 142 boolean trackProgress = !buffer.isTemporary() && length != 0; 144 145 if(trackProgress) 146 { 147 setMaximum(length); 148 setValue(0); 149 } 150 151 if(length == 0) 154 length = IOBUFSIZE; 155 156 SegmentBuffer seg = new SegmentBuffer((int)length + 1); 157 158 char[] buf = new char[IOBUFSIZE]; 159 160 int len; 165 166 boolean CRLF = false; 169 170 boolean CROnly = false; 172 173 boolean lastWasCR = false; 177 178 int lineCount = 0; 181 182 while((len = in.read(buf,0,buf.length)) != -1) 183 { 184 int lastLine = 0; 188 189 for(int i = 0; i < len; i++) 190 { 191 switch(buf[i]) 193 { 194 case '\r': 195 if(lastWasCR) 200 { 201 CROnly = true; 202 CRLF = false; 203 } 204 else 208 { 209 lastWasCR = true; 210 } 211 212 seg.append(buf,lastLine,i - 214 lastLine); 215 seg.append('\n'); 216 endOffsets.add(seg.count); 217 if(trackProgress && lineCount++ % PROGRESS_INTERVAL == 0) 218 setValue(seg.count); 219 220 lastLine = i + 1; 223 break; 224 case '\n': 225 if(lastWasCR) 233 { 234 CROnly = false; 235 CRLF = true; 236 lastWasCR = false; 237 lastLine = i + 1; 242 } 243 else 248 { 249 CROnly = false; 250 CRLF = false; 251 seg.append(buf,lastLine, 252 i - lastLine); 253 seg.append('\n'); 254 endOffsets.add(seg.count); 255 if(trackProgress && lineCount++ % PROGRESS_INTERVAL == 0) 256 setValue(seg.count); 257 lastLine = i + 1; 258 } 259 break; 260 default: 261 if(lastWasCR) 267 { 268 CROnly = true; 269 CRLF = false; 270 lastWasCR = false; 271 } 272 break; 273 } 274 } 275 276 if(trackProgress) 277 setValue(seg.count); 278 279 seg.append(buf,lastLine,len - lastLine); 281 } 282 283 setAbortable(false); 284 285 String lineSeparator; 286 if(seg.count == 0) 287 { 288 lineSeparator = jEdit.getProperty( 291 "buffer.lineSeparator", 292 System.getProperty("line.separator")); 293 } 294 else if(CRLF) 295 lineSeparator = "\r\n"; 296 else if(CROnly) 297 lineSeparator = "\r"; 298 else 299 lineSeparator = "\n"; 300 301 in.close(); 302 303 int bufferLength = seg.count; 305 if(bufferLength != 0) 306 { 307 char ch = seg.array[bufferLength - 1]; 308 if(ch == 0x1a ) 309 seg.count--; 310 } 311 312 buffer.setBooleanProperty(Buffer.TRAILING_EOL,false); 313 if(bufferLength != 0 && jEdit.getBooleanProperty("stripTrailingEOL")) 314 { 315 char ch = seg.array[bufferLength - 1]; 316 if(ch == '\n') 317 { 318 buffer.setBooleanProperty(Buffer.TRAILING_EOL,true); 319 seg.count--; 320 endOffsets.setSize(endOffsets.getSize() - 1); 321 } 322 } 323 324 endOffsets.add(seg.count + 1); 327 328 if(!insert) 332 { 333 buffer.setProperty(LOAD_DATA,seg); 334 buffer.setProperty(END_OFFSETS,endOffsets); 335 buffer.setProperty(NEW_PATH,path); 336 if(lineSeparator != null) 337 buffer.setProperty(JEditBuffer.LINESEP,lineSeparator); 338 } 339 340 return seg; 342 } 344 protected void write(Buffer buffer, OutputStream out) 346 throws IOException 347 { 348 try 349 { 350 out = new BufferedOutputStream(out); 351 String encoding = buffer.getStringProperty(JEditBuffer.ENCODING); 352 if(encoding.equals(MiscUtilities.UTF_8_Y)) 353 { 354 out.write(UTF8_MAGIC_1); 356 out.write(UTF8_MAGIC_2); 357 out.write(UTF8_MAGIC_3); 358 out.flush(); 359 encoding = "UTF-8"; 360 } 361 else if (encoding.equals("UTF-16LE")) 362 { 363 out.write(UNICODE_MAGIC_2); 364 out.write(UNICODE_MAGIC_1); 365 out.flush(); 366 } 367 else if (encoding.equals("UTF-16BE")) 368 { 369 out.write(UNICODE_MAGIC_1); 370 out.write(UNICODE_MAGIC_2); 371 out.flush(); 372 } 373 CharsetEncoder encoder = Charset.forName(encoding).newEncoder(); 374 375 Segment lineSegment = new Segment (); 376 String newline = buffer.getStringProperty(JEditBuffer.LINESEP); 377 if(newline == null) 378 newline = System.getProperty("line.separator"); 379 ByteBuffer newlineBuffer = encoder.encode(CharBuffer.wrap(newline)); 382 byte[] newlineBytes = new byte[newlineBuffer.limit()]; 383 newlineBuffer.get(newlineBytes); 384 encoder.reset(); 385 386 ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream(IOBUFSIZE); 391 Writer lineWriter = new OutputStreamWriter(lineBuffer, encoder); 396 397 setMaximum(buffer.getLineCount() / PROGRESS_INTERVAL); 398 setValue(0); 399 400 int i = 0; 401 while(i < buffer.getLineCount()) 402 { 403 buffer.getLineText(i,lineSegment); 404 try 405 { 406 lineWriter.write(lineSegment.array, 407 lineSegment.offset, 408 lineSegment.count); 409 lineWriter.flush(); 410 } 411 catch(CharacterCodingException e) 412 { 413 String message = "Failed to encode the line " + (i + 1); 414 IOException wrapping = new CharConversionException(message); 415 wrapping.initCause(e); 416 throw wrapping; 417 } 418 lineBuffer.writeTo(out); 419 lineBuffer.reset(); 420 421 if(i != buffer.getLineCount() - 1) 422 { 423 out.write(newlineBytes); 424 } 425 426 if(++i % PROGRESS_INTERVAL == 0) 427 setValue(i / PROGRESS_INTERVAL); 428 } 429 430 if(jEdit.getBooleanProperty("stripTrailingEOL") 431 && buffer.getBooleanProperty(Buffer.TRAILING_EOL)) 432 { 433 out.write(newlineBytes); 434 } 435 } 436 finally 437 { 438 IOUtilities.closeQuietly(out); 439 } 440 } 442 } 444 | Popular Tags |