1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 * Benjamin Muskalla <b.muskalla@gmx.net> - [spell checking][implementation] PersistentSpellDictionary closes wrong stream - https://bugs.eclipse.org/bugs/show_bug.cgi?id=236421
11 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.text.spelling.engine;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.charset.Charset;
20 import org.eclipse.jdt.internal.ui.JavaPlugin;
24 * Persistent modifiable word-list based dictionary.
28 public class PersistentSpellDictionary extends AbstractSpellDictionary {
30 /** The word list location */
31 private final URL fLocation;
34 * Creates a new persistent spell dictionary.
36 * @param url the URL of the word list for this dictionary
38 public PersistentSpellDictionary(final URL url) {
43 * @see org.eclipse.jdt.ui.text.spelling.engine.AbstractSpellDictionary#acceptsWords()
46 public boolean acceptsWords() {
51 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellDictionary#addWord(java.lang.String)
54 public void addWord(final String word) {
58 FileOutputStream fileStream= null;
60 Charset charset= Charset.forName(getEncoding());
61 ByteBuffer byteBuffer= charset.encode(word + "\n"); //$NON-NLS-1$
62 int size= byteBuffer.limit();
63 final byte[] byteArray;
64 if (byteBuffer.hasArray())
65 byteArray= byteBuffer.array();
67 byteArray= new byte[size];
68 byteBuffer.get(byteArray);
71 fileStream= new FileOutputStream(fLocation.getPath(), true);
73 // Encoding UTF-16 charset writes a BOM. In which case we need to cut it away if the file isn't empty
75 if (!isEmpty() && "UTF-16".equals(charset.name())) //$NON-NLS-1$
78 fileStream.write(byteArray, bomCutSize, size - bomCutSize);
79 } catch (IOException exception) {
80 JavaPlugin.log(exception);
84 if (fileStream != null)
86 } catch (IOException e) {
94 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.AbstractSpellDictionary#getURL()
97 protected final URL getURL() {