diff options
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite')
-rw-r--r-- | core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/util/FileContentHelper.java | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/util/FileContentHelper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/util/FileContentHelper.java index 6c8aaa8c51f..8fc40a49d76 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/util/FileContentHelper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/util/FileContentHelper.java @@ -7,7 +7,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation ******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.util; @@ -22,33 +22,29 @@ import org.eclipse.core.runtime.CoreException; /** * @author Emanuel Graf IFS - * */ public class FileContentHelper { - - private static final int bufferSize = 512; + private static final int BUFFER_SIZE = 2048; - public static String getContent(IFile file, int start) throws CoreException, IOException{ - + public static String getContent(IFile file, int start) throws CoreException, IOException { InputStreamReader reader = getReaderForFile(file); skip(start, reader); final String rest = readRest(reader); reader.close(); return rest; - } public static String getContent(IFile file, int start, int length) { try { InputStreamReader r = getReaderForFile(file); - char[] bytes = new char[length]; + char[] chars = new char[length]; skip(start, r); - read(length, r, bytes); + read(length, r, chars); r.close(); - return new String(bytes); + return new String(chars); } catch (IOException e) { CCorePlugin.log(e); } catch (CoreException e) { @@ -60,29 +56,26 @@ public class FileContentHelper { private static InputStreamReader getReaderForFile(IFile file) throws CoreException, UnsupportedEncodingException { InputStream contents = file.getContents(); - InputStreamReader r = new InputStreamReader(contents, file.getCharset()); - return r; + return new InputStreamReader(contents, file.getCharset()); } - private static String readRest(InputStreamReader reader) throws IOException{ + private static String readRest(InputStreamReader reader) throws IOException { StringBuilder content = new StringBuilder(); - char[] buffer = new char[bufferSize]; + char[] buffer = new char[BUFFER_SIZE]; int bytesRead = 0; - while((bytesRead = reader.read(buffer)) >= 0){ + while ((bytesRead = reader.read(buffer)) >= 0) { content.append(buffer, 0, bytesRead); } - return content.toString(); } - private static void read(int length, InputStreamReader r, char[] bytes) - throws IOException { + private static void read(int length, InputStreamReader r, char[] bytes) throws IOException { int bufferOffset = 0; int charactersRead = 0; - while(charactersRead >= 0 && length > 0){ + while (charactersRead >= 0 && length > 0) { charactersRead = r.read(bytes, bufferOffset, length); - if(charactersRead > 0){ + if (charactersRead > 0) { bufferOffset += charactersRead; length -= charactersRead; } @@ -91,12 +84,11 @@ public class FileContentHelper { private static void skip(int start, InputStreamReader r) throws IOException { long skipped = 0; - while(skipped >= 0 && start > 0 && r.ready()){ + while (skipped >= 0 && start > 0 && r.ready()) { skipped = r.skip(start); - if(skipped > 0){ + if (skipped > 0) { start -= skipped; } } } - } |