Harden charset-as-string taking function deprecations.

Fix usages in compiler.
This commit is contained in:
Ilya Gorbunov
2016-01-19 20:54:29 +03:00
parent 6d5a65cac6
commit c1ad82ff8c
9 changed files with 32 additions and 34 deletions
@@ -406,7 +406,7 @@ abstract class KotlinDebuggerTestBase : KotlinDebuggerTestCase() {
val sourceFiles = runReadAction {
FilenameIndex.getAllFilesByExt(project, "kt").filter {
it.name.contains(fileName) &&
it.contentsToByteArray().toString("UTF-8").contains(lineMarker)
it.contentsToByteArray().toString(Charsets.UTF_8).contains(lineMarker)
}
}
@@ -9,8 +9,8 @@ import java.nio.charset.Charset
/**
* Converts the contents of this byte array to a string using the specified [charset].
*/
@Deprecated("Use ByteArray.toString(Charset) instead.", ReplaceWith("this.toString(charset(charset))"))
public fun ByteArray.toString(charset: String): String = String(this, charset)
@Deprecated("Use ByteArray.toString(Charset) instead.", ReplaceWith("this.toString(charset(charset))"), level = DeprecationLevel.ERROR)
public fun ByteArray.toString(charset: String): String = String(this, charset(charset))
/**
* Converts the contents of this byte array to a string using the specified [charset].
@@ -69,8 +69,8 @@ public fun File.appendBytes(array: ByteArray): Unit = FileOutputStream(this, tru
* @param charset character set to use.
* @return the entire content of this file as a String.
*/
@Deprecated("Use File.readText(Charset) instead.", ReplaceWith("this.readText(charset(charset))"))
public fun File.readText(charset: String): String = readBytes().toString(charset)
@Deprecated("Use File.readText(Charset) instead.", ReplaceWith("this.readText(charset(charset))"), level = DeprecationLevel.ERROR)
public fun File.readText(charset: String): String = readBytes().toString(charset(charset))
/**
* Gets the entire content of this file as a String using UTF-8 or specified [charset].
@@ -89,8 +89,8 @@ public fun File.readText(charset: Charset = Charsets.UTF_8): String = readBytes(
* @param text text to write into file.
* @param charset character set to use.
*/
@Deprecated("Use File.writeText(String, Charset) instead.", ReplaceWith("this.writeText(text, charset(charset))"))
public fun File.writeText(text: String, charset: String): Unit = writeBytes(text.toByteArray(charset))
@Deprecated("Use File.writeText(String, Charset) instead.", ReplaceWith("this.writeText(text, charset(charset))"), level = DeprecationLevel.ERROR)
public fun File.writeText(text: String, charset: String): Unit = writeBytes(text.toByteArray(charset(charset)))
/**
* Sets the content of this file as [text] encoded using UTF-8 or specified [charset].
@@ -115,8 +115,8 @@ public fun File.appendText(text: String, charset: Charset = Charsets.UTF_8): Uni
* @param text text to append to file.
* @param charset character set to use.
*/
@Deprecated("Use File.appendText(String, Charset) instead.", ReplaceWith("this.appendText(text, charset(charset))"))
public fun File.appendText(text: String, charset: String): Unit = appendBytes(text.toByteArray(charset))
@Deprecated("Use File.appendText(String, Charset) instead.", ReplaceWith("this.appendText(text, charset(charset))"), level = DeprecationLevel.ERROR)
public fun File.appendText(text: String, charset: String): Unit = appendBytes(text.toByteArray(charset(charset)))
/**
* Reads file by byte blocks and calls [action] for each block read.
@@ -178,7 +178,7 @@ public fun File.forEachLine(charset: Charset = Charsets.UTF_8, action: (line: St
* @param charset character set to use.
* @param operation function to process file lines.
*/
@Deprecated("Use File.forEachLine(Charset, operation) instead.", ReplaceWith("this.forEachLine(charset(charset), operation)"))
@Deprecated("Use File.forEachLine(Charset, operation) instead.", ReplaceWith("this.forEachLine(charset(charset), operation)"), level = DeprecationLevel.ERROR)
public fun File.forEachLine(charset: String, operation: (line: String) -> Unit): Unit = forEachLine(Charset.forName(charset), operation)
/**
@@ -189,7 +189,7 @@ public fun File.forEachLine(charset: String, operation: (line: String) -> Unit):
* @param charset character set to use.
* @return list of file lines.
*/
@Deprecated("Use File.readLines(Charset) instead.", ReplaceWith("this.readLines(charset(charset))"))
@Deprecated("Use File.readLines(Charset) instead.", ReplaceWith("this.readLines(charset(charset))"), level = DeprecationLevel.ERROR)
public fun File.readLines(charset: String): List<String> = readLines(Charset.forName(charset))
/**
+6 -6
View File
@@ -69,12 +69,12 @@ public fun InputStream.reader(charset: Charset = Charsets.UTF_8): InputStreamRea
public fun InputStream.bufferedReader(charset: Charset = Charsets.UTF_8): BufferedReader = reader(charset).buffered()
/** Creates a reader on this input stream using the specified [charset]. */
@Deprecated("Use InputStream.reader(Charset) instead.", ReplaceWith("this.reader(charset(charset))"))
@Deprecated("Use InputStream.reader(Charset) instead.", ReplaceWith("this.reader(charset(charset))"), level = DeprecationLevel.ERROR)
public fun InputStream.reader(charset: String): InputStreamReader = InputStreamReader(this, charset)
/** Creates a buffered reader on this input stream using the specified [charset]. */
@Deprecated("Use InputStream.bufferedReader(Charset) instead.", ReplaceWith("this.bufferedReader(charset(charset))"))
public fun InputStream.bufferedReader(charset: String): BufferedReader = reader(charset).buffered()
@Deprecated("Use InputStream.bufferedReader(Charset) instead.", ReplaceWith("this.bufferedReader(charset(charset))"), level = DeprecationLevel.ERROR)
public fun InputStream.bufferedReader(charset: String): BufferedReader = reader(charset(charset)).buffered()
/**
* Creates a buffered output stream wrapping this stream.
@@ -90,12 +90,12 @@ public fun OutputStream.writer(charset: Charset = Charsets.UTF_8): OutputStreamW
public fun OutputStream.bufferedWriter(charset: Charset = Charsets.UTF_8): BufferedWriter = writer(charset).buffered()
/** Creates a writer on this output stream using the specified [charset]. */
@Deprecated("Use OutputStream.writer(Charset) instead.", ReplaceWith("this.writer(charset(charset))"))
@Deprecated("Use OutputStream.writer(Charset) instead.", ReplaceWith("this.writer(charset(charset))"), level = DeprecationLevel.ERROR)
public fun OutputStream.writer(charset: String): OutputStreamWriter = OutputStreamWriter(this, charset)
/** Creates a buffered writer on this output stream using the specified [charset]. */
@Deprecated("Use OutputStream.bufferedWriter(Charset) instead.", ReplaceWith("this.bufferedWriter(charset(charset))"))
public fun OutputStream.bufferedWriter(charset: String): BufferedWriter = writer(charset).buffered()
@Deprecated("Use OutputStream.bufferedWriter(Charset) instead.", ReplaceWith("this.bufferedWriter(charset(charset))"), level = DeprecationLevel.ERROR)
public fun OutputStream.bufferedWriter(charset: String): BufferedWriter = writer(charset(charset)).buffered()
/**
* Copies this stream to the given output stream, returning the number of bytes copied
+2 -2
View File
@@ -127,8 +127,8 @@ public fun Reader.copyTo(out: Writer, bufferSize: Int = DEFAULT_BUFFER_SIZE): Lo
* @param charset a character set to use.
* @return a string with this URL entire content.
*/
@Deprecated("Use URL.readText(Charset) instead.", ReplaceWith("this.readText(charset(charset))"))
public fun URL.readText(charset: String): String = readBytes().toString(charset)
@Deprecated("Use URL.readText(Charset) instead.", ReplaceWith("this.readText(charset(charset))"), level = DeprecationLevel.ERROR)
public fun URL.readText(charset: String): String = readBytes().toString(charset(charset))
/**
* Reads the entire content of this URL as a String using UTF-8 or the specified [charset].
@@ -181,7 +181,7 @@ public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean
* @param length the number of bytes to be converted.
* @param charsetName the name of the character set to use.
*/
@Deprecated("Use String(bytes, offset, length, Charset) instead.", ReplaceWith("String(bytes, offset, length, charset(charsetName))"))
@Deprecated("Use String(bytes, offset, length, Charset) instead.", ReplaceWith("String(bytes, offset, length, charset(charsetName))"), level = DeprecationLevel.ERROR)
public fun String(bytes: ByteArray, offset: Int, length: Int, charsetName: String): String = java.lang.String(bytes, offset, length, charsetName) as String
/**
@@ -199,7 +199,7 @@ public fun String(bytes: ByteArray, offset: Int, length: Int, charset: Charset):
* Converts the data from the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*/
@Deprecated("Use String(bytes, Charset) instead.", ReplaceWith("String(bytes, charset(charsetName))"))
@Deprecated("Use String(bytes, Charset) instead.", ReplaceWith("String(bytes, charset(charsetName))"), level = DeprecationLevel.ERROR)
public fun String(bytes: ByteArray, charsetName: String): String = java.lang.String(bytes, charsetName) as String
/**
@@ -412,7 +412,7 @@ public operator fun CharSequence.get(start: Int, end: Int): CharSequence = subSe
/**
* Encodes the contents of this string using the specified character set and returns the resulting byte array.
*/
@Deprecated("Use String.toByteArray(Charset) instead.", ReplaceWith("this.toByteArray(charset(charset))"))
@Deprecated("Use String.toByteArray(Charset) instead.", ReplaceWith("this.toByteArray(charset(charset))"), level = DeprecationLevel.ERROR)
public fun String.toByteArray(charset: String): ByteArray = (this as java.lang.String).getBytes(charset)
/**
+4 -4
View File
@@ -352,19 +352,19 @@ class FilesTest {
@test fun testCopyTo() {
val srcFile = createTempFile()
val dstFile = createTempFile()
srcFile.writeText("Hello, World!", "UTF8")
srcFile.writeText("Hello, World!")
assertFailsWith(FileAlreadyExistsException::class) {
srcFile.copyTo(dstFile)
}
var len = srcFile.copyTo(dstFile, overwrite = true)
assertEquals(13L, len)
assertEquals(srcFile.readText(), dstFile.readText("UTF8"))
assertEquals(srcFile.readText(), dstFile.readText(Charsets.UTF_8))
assertTrue(dstFile.delete())
len = srcFile.copyTo(dstFile)
assertEquals(13L, len)
assertEquals(srcFile.readText("UTF8"), dstFile.readText())
assertEquals(srcFile.readText(Charsets.UTF_8), dstFile.readText())
assertTrue(dstFile.delete())
dstFile.mkdir()
@@ -395,7 +395,7 @@ class FilesTest {
val srcFile = createTempFile()
val dstFile = createTempFile(directory = currentDir)
try {
srcFile.writeText("Hello, World!", "UTF8")
srcFile.writeText("Hello, World!", Charsets.UTF_8)
dstFile.delete()
val dstRelative = File(dstFile.name)
+4 -4
View File
@@ -15,12 +15,12 @@ fun sample(): Reader = StringReader("Hello\nWorld");
class ReadWriteTest {
@test fun testAppendText() {
val file = File.createTempFile("temp", System.nanoTime().toString())
file.writeText("Hello\n", "UTF8")
file.appendText("World\n", "UTF8")
file.writeText("Hello\n")
file.appendText("World\n")
file.appendText("Again")
assertEquals("Hello\nWorld\nAgain", file.readText())
assertEquals(listOf("Hello", "World", "Again"), file.readLines("UTF8"))
assertEquals(listOf("Hello", "World", "Again"), file.readLines(Charsets.UTF_8))
file.deleteOnExit()
}
@@ -145,7 +145,7 @@ class ReadWriteTest {
val url = URL("http://kotlinlang.org")
val text = url.readText()
assertFalse(text.isEmpty())
val text2 = url.readText("UTF8")
val text2 = url.readText(charset("UTF8"))
assertFalse(text2.isEmpty())
}
}
+2 -4
View File
@@ -74,10 +74,8 @@ class StringJVMTest {
}
@test fun toByteArrayEncodings() {
val s = "hello"
val defaultCharset = java.nio.charset.Charset.defaultCharset()!!
assertEquals(String(s.toByteArray()), String(s.toByteArray(defaultCharset)))
assertEquals(String(s.toByteArray()), String(s.toByteArray(defaultCharset.name())))
val s = "hello®"
assertEquals(String(s.toByteArray()), String(s.toByteArray(Charsets.UTF_8)))
}
@test fun toCharArray() {