From efe85b5b80cb8a39f0b1c98ef69314deea32a03b Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Thu, 15 Aug 2019 18:30:48 -0500 Subject: [PATCH] Convert MarkdownFile to Kotlin --- .../simplemarkdown/model/MarkdownFile.java | 83 ------------------- .../simplemarkdown/model/MarkdownFile.kt | 61 ++++++++++++++ 2 files changed, 61 insertions(+), 83 deletions(-) delete mode 100644 app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.java create mode 100644 app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.kt diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.java b/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.java deleted file mode 100644 index 9a42f73..0000000 --- a/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.wbrawner.simplemarkdown.model; - -import com.wbrawner.simplemarkdown.utility.ErrorHandler; -import com.wbrawner.simplemarkdown.utility.Utils; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; - -/** - * This class serves as a wrapper to manage the manage the file input and output operations, as well - * as to keep track of the data itself in memory. - */ -public class MarkdownFile { - private String name; - private String content; - private final ErrorHandler errorHandler; - - public MarkdownFile(ErrorHandler errorHandler, String name, String content) { - this.errorHandler = errorHandler; - this.name = name; - this.content = content; - } - - - public MarkdownFile(ErrorHandler errorHandler) { - this.errorHandler = errorHandler; - this.name = "Untitled.md"; - this.content = ""; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public boolean load(String name, InputStream in) { - StringBuilder sb = new StringBuilder(); - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(in)); - String line; - while ((line = reader.readLine()) != null) { - sb.append(line).append('\n'); - } - this.name = name; - this.content = sb.toString(); - return true; - } catch (IOException ignored) { - return false; - } finally { - Utils.closeQuietly(reader); - } - } - - public boolean save(String name, OutputStream outputStream) { - OutputStreamWriter writer = null; - try { - writer = new OutputStreamWriter(outputStream); - writer.write(this.content); - this.name = name; - } catch (IOException ignored) { - return false; - } finally { - Utils.closeQuietly(writer); - } - return true; - } -} diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.kt b/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.kt new file mode 100644 index 0000000..8356138 --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/model/MarkdownFile.kt @@ -0,0 +1,61 @@ +package com.wbrawner.simplemarkdown.model + +import com.wbrawner.simplemarkdown.utility.ErrorHandler +import com.wbrawner.simplemarkdown.utility.Utils +import java.io.* + +/** + * This class serves as a wrapper to manage the manage the file input and output operations, as well + * as to keep track of the data itself in memory. + */ +class MarkdownFile { + var name: String? = null + var content: String? = null + private val errorHandler: ErrorHandler + + constructor(errorHandler: ErrorHandler, name: String, content: String) { + this.errorHandler = errorHandler + this.name = name + this.content = content + } + + + constructor(errorHandler: ErrorHandler) { + this.errorHandler = errorHandler + this.name = "Untitled.md" + this.content = "" + } + + fun load(name: String, `in`: InputStream): Boolean { + val sb = StringBuilder() + var reader: BufferedReader? = null + try { + reader = BufferedReader(InputStreamReader(`in`)) + var line: String + while ((line = reader.readLine()) != null) { + sb.append(line).append('\n') + } + this.name = name + this.content = sb.toString() + return true + } catch (ignored: IOException) { + return false + } finally { + Utils.closeQuietly(reader) + } + } + + fun save(name: String, outputStream: OutputStream): Boolean { + var writer: OutputStreamWriter? = null + try { + writer = OutputStreamWriter(outputStream) + writer.write(this.content) + this.name = name + } catch (ignored: IOException) { + return false + } finally { + Utils.closeQuietly(writer) + } + return true + } +}