-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* escape in json * Update CountingCsvWriter.scala * better design * rename * tests * fix test
- Loading branch information
Showing
5 changed files
with
195 additions
and
143 deletions.
There are no files selected for viewing
61 changes: 0 additions & 61 deletions
61
connector/src/main/scala/com/microsoft/kusto/spark/datasink/CountingCsvWriter.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
connector/src/main/scala/com/microsoft/kusto/spark/datasink/Writers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.microsoft.kusto.spark.datasink | ||
|
||
trait Writer { | ||
val out: java.io.Writer | ||
def write(c: Char): Unit | ||
def write(str: String): Unit | ||
def writeStringField(str: String): Unit | ||
def writeUnescaped(str: String): Unit = { | ||
out.write(str) | ||
} | ||
} | ||
|
||
case class CountingWriter(out: java.io.Writer) extends Writer { | ||
val newLineSep: String = java.security.AccessController.doPrivileged( | ||
new sun.security.action.GetPropertyAction("line.separator")) | ||
val newLineSepLength: Int = newLineSep.length | ||
var bytesCounter: Long = 0L | ||
|
||
def newLine(): Unit = { | ||
out.write(newLineSep) | ||
bytesCounter += newLineSepLength | ||
} | ||
|
||
def write(c: Char): Unit ={ | ||
out.write(c) | ||
bytesCounter += 1 | ||
} | ||
def write(str: String): Unit = { | ||
out.write(str) | ||
bytesCounter += str.length | ||
} | ||
|
||
def writeStringField(str: String) { | ||
if (str.length > 0) { | ||
out.write('"') | ||
bytesCounter += 2 | ||
for (c <- str) { | ||
if (c == '"') { | ||
out.write("\"\"") | ||
bytesCounter += 1 | ||
} else { | ||
out.write(c) | ||
} | ||
} | ||
|
||
out.write('"') | ||
bytesCounter += str.length | ||
} | ||
} | ||
|
||
def getCounter: Long = bytesCounter | ||
|
||
def resetCounter(): Unit = { | ||
bytesCounter = 0 | ||
} | ||
} | ||
|
||
case class EscapedWriter(out: java.io.Writer) extends Writer { | ||
def write(c: Char): Unit ={ | ||
out.write(c) | ||
} | ||
|
||
def write(str: String): Unit ={ | ||
for (c <- str) { | ||
val escaped = EscapedWriter.escapeTable(c) | ||
if (escaped != 0) { | ||
out.write('\\') | ||
out.write(escaped) | ||
} else { | ||
out.write(c) | ||
} | ||
} | ||
} | ||
|
||
def writeStringField(str: String): Unit = { | ||
if (str.length > 0) { | ||
out.write('"') | ||
write(str) | ||
out.write('"') | ||
} | ||
} | ||
} | ||
|
||
object EscapedWriter { | ||
val escapeTable: Array[Int] = Array.fill[Int](128)(0) | ||
escapeTable('"') = '"' | ||
escapeTable('\\') = '\\' | ||
escapeTable('\n') = 'n' | ||
escapeTable('\r') = 'r' | ||
escapeTable('\b') = 'b' | ||
escapeTable('\t') = 't' | ||
escapeTable('\f') = 'f' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.