1 11 12 package org.eclipse.jdt.internal.ui.text.spelling.engine; 13 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStreamWriter ; 17 import java.net.URL ; 18 import java.nio.ByteBuffer ; 19 import java.nio.charset.Charset ; 20 21 import org.eclipse.jdt.internal.ui.JavaPlugin; 22 23 24 29 public class PersistentSpellDictionary extends AbstractSpellDictionary { 30 31 32 private final URL fLocation; 33 34 40 public PersistentSpellDictionary(final URL url) { 41 fLocation= url; 42 } 43 44 47 public boolean acceptsWords() { 48 return true; 49 } 50 51 54 public void addWord(final String word) { 55 if (isCorrect(word)) 56 return; 57 58 OutputStreamWriter writer= null; 59 try { 60 Charset charset= Charset.forName(getEncoding()); 61 ByteBuffer byteBuffer= charset.encode(word + "\n"); int size= byteBuffer.limit(); 63 final byte[] byteArray; 64 if (byteBuffer.hasArray()) 65 byteArray= byteBuffer.array(); 66 else { 67 byteArray= new byte[size]; 68 byteBuffer.get(byteArray); 69 } 70 71 FileOutputStream fileStream= new FileOutputStream (fLocation.getPath(), true); 72 73 int bomCutSize= 0; 75 if (!isEmpty() && "UTF-16".equals(charset.name())) bomCutSize= 2; 77 78 fileStream.write(byteArray, bomCutSize, size - bomCutSize); 79 } catch (IOException exception) { 80 JavaPlugin.log(exception); 81 return; 82 } finally { 83 try { 84 if (writer != null) 85 writer.close(); 86 } catch (IOException e) { 87 } 88 } 89 90 hashWord(word); 91 } 92 93 96 protected final URL getURL() { 97 return fLocation; 98 } 99 } 100 | Popular Tags |