@@ -434,7 +434,6 @@ public void load(FileLocation loc) throws IOException {
434434 load (loc , (String ) null );
435435 }
436436
437-
438437 /**
439438 * Loads the specified file in this editor. This method fires a property
440439 * change event of type {@link #FULL_PATH_PROPERTY}.
@@ -447,61 +446,61 @@ public void load(FileLocation loc) throws IOException {
447446 * is used.
448447 * @throws IOException If an IO error occurs.
449448 * @see #load(FileLocation)
450- * @see #load(FileLocation, String )
449+ * @see #load(FileLocation, Charset )
451450 * @see #save()
452451 * @see #saveAs(FileLocation)
453452 */
454- public void load (FileLocation loc , Charset defaultEnc ) throws IOException {
455- load (loc , defaultEnc == null ? null : defaultEnc . name ( ));
453+ public void load (FileLocation loc , String defaultEnc ) throws IOException {
454+ load (loc , defaultEnc == null ? Charset . defaultCharset () : Charset . forName ( defaultEnc ));
456455 }
457456
458-
459457 /**
460458 * Loads the specified file in this editor. This method fires a property
461459 * change event of type {@link #FULL_PATH_PROPERTY}.
462460 *
463- * @param loc The location of the file to load. This cannot be
464- * <code>null</code>.
465- * @param defaultEnc The encoding to use when loading/saving the file.
466- * This encoding will only be used if the file is not Unicode.
467- * If this value is <code>null</code>, the system default encoding
468- * is used.
461+ * @param loc The location of the file to load. This cannot be
462+ * <code>null</code>.
463+ * @param defaultCharset The encoding to use when loading/saving the file.
464+ * This encoding will only be used if the file is not Unicode.
465+ * If this value is <code>null</code>, the system default encoding
466+ * is used.
469467 * @throws IOException If an IO error occurs.
470468 * @see #load(FileLocation)
471- * @see #load(FileLocation, Charset )
469+ * @see #load(FileLocation, String )
472470 * @see #save()
473471 * @see #saveAs(FileLocation)
474472 */
475- public void load (FileLocation loc , String defaultEnc ) throws IOException {
476- RSyntaxDocument doc = createEditableDocument (loc , defaultEnc );
477- loadDocument (loc , doc );
473+ public void load (FileLocation loc , Charset defaultCharset ) throws IOException {
474+ Charset fileLocationCharset = getFileLocationCharset (loc , defaultCharset );
475+ RSyntaxDocument doc = createEditableDocument (loc , fileLocationCharset );
476+ loadDocument (loc , doc , defaultCharset );
478477 }
479478
480479 public void loadLocalFileInReadOnlyDocument (File file , Charset defaultCharset ) throws IOException {
481480 FileLocation fileLocation = FileLocation .create (file );
482- RSyntaxDocument doc = createLazyLoadDocument (fileLocation , defaultCharset );
481+ Charset fileLocationCharset = getFileLocationCharset (fileLocation , defaultCharset );
482+ RSyntaxDocument doc = createLazyLoadDocument (fileLocation , fileLocationCharset );
483+ loadDocument (fileLocation , doc , defaultCharset );
484+ }
483485
484- loadDocument (fileLocation , doc );
486+ private Charset getFileLocationCharset (FileLocation fileLocation , Charset defaultCharset ) throws IOException {
487+ try ( InputStream is = fileLocation .getInputStream () ){
488+ UnicodeReader ur = new UnicodeReader (is , defaultCharset );
489+ defaultCharset = Charset .forName (ur .getEncoding ());
490+ }
491+ return defaultCharset ;
485492 }
486493
487- private RSyntaxDocument createEditableDocument (FileLocation loc , String defaultEnc ) throws IOException {
494+ private RSyntaxDocument createEditableDocument (FileLocation loc , Charset charset ) throws IOException {
488495 RSyntaxDocument doc = createDefaultModel ();
489496 // For new local files, just go with it.
490- if ( loc .isLocal () && !loc .isLocalAndExists () ){
491- this .charSet = defaultEnc != null ? defaultEnc : getDefaultEncoding ();
492- } else {
493- // Old local files and remote files, load 'em up. UnicodeReader will
494- // check for BOMs and handle them correctly in all cases, then pass
495- // rest of stream down to InputStreamReader.
496- UnicodeReader ur = new UnicodeReader (loc .getInputStream (), defaultEnc );
497- charSet = ur .getEncoding ();
498-
499- try ( BufferedReader r = new BufferedReader (ur ) ){
500- RTextAreaEditorKit kit = (RTextAreaEditorKit )getUI ().getEditorKit (this );
501- try {
497+ if ( !loc .isLocal () || loc .isLocalAndExists () ){
498+ try ( InputStreamReader r = new InputStreamReader (loc .getInputStream (), charset ) ){
499+ RTextAreaEditorKit kit = (RTextAreaEditorKit ) getUI ().getEditorKit (this );
500+ try {
502501 // NOTE: Resets the "line separator" property.
503502 kit .read (r , doc , 0 );
504- } catch ( BadLocationException e ) {
503+ } catch ( BadLocationException e ) {
505504 throw new IOException (e .getMessage ());
506505 }
507506 }
@@ -510,20 +509,15 @@ private RSyntaxDocument createEditableDocument(FileLocation loc, String defaultE
510509 return doc ;
511510 }
512511
513- private RSyntaxDocument createLazyLoadDocument (FileLocation fileLocation , Charset defaultCharset ) throws IOException {
512+ private RSyntaxDocument createLazyLoadDocument (FileLocation fileLocation , Charset charSet ) throws IOException {
514513 if ( !fileLocation .isLocalAndExists () ){
515514 throw new FileNotFoundException ("ReadOnlyDucument supports only files from local file system" );
516515 }
517-
518- try (InputStream is = fileLocation .getInputStream ()) {
519- UnicodeReader ur = new UnicodeReader (is , defaultCharset );
520- charSet = ur .getEncoding ();
521- return new ReadOnlyDocument (getTokenMarkerFactory (), getSyntaxEditingStyle (), new ReadOnlyContent (new File (fileLocation .getFileFullPath ()), Charset .forName (charSet )));
522- }
523-
516+ return new ReadOnlyDocument (getTokenMarkerFactory (), getSyntaxEditingStyle (), new ReadOnlyContent (new File (fileLocation .getFileFullPath ()), charSet ));
524517 }
525518
526- public void loadDocument (FileLocation fileLocation , RSyntaxDocument doc ) {
519+ public void loadDocument (FileLocation fileLocation , RSyntaxDocument doc , Charset charSet ) {
520+ this .charSet = charSet .name ();
527521 String oldFileFullPath = getFileFullPath ();
528522 setDocument (doc );
529523 this .loc = fileLocation ;
0 commit comments