Fix test behaviour of OutputStreamWriterTest and WriterTest.

Change-Id: Ief4c101813f3d1ecb2058c877d2fb757400abdf1
diff --git a/user/super/com/google/gwt/emul/java/io/Writer.java b/user/super/com/google/gwt/emul/java/io/Writer.java
index 4bba793..a2e34e2 100644
--- a/user/super/com/google/gwt/emul/java/io/Writer.java
+++ b/user/super/com/google/gwt/emul/java/io/Writer.java
@@ -15,8 +15,8 @@
  */
 package java.io;
 
+import static javaemul.internal.InternalPreconditions.checkCriticalStringBounds;
 import static javaemul.internal.InternalPreconditions.checkNotNull;
-import static javaemul.internal.InternalPreconditions.checkPositionIndexes;
 
 import java.util.Objects;
 
@@ -57,7 +57,6 @@
   }
 
   public void write(String str, int offset, int count) throws IOException {
-    IOUtils.checkOffsetAndCount(str, offset, count);
     char[] buf = new char[count];
     str.getChars(offset, offset + count, buf, 0);
     write(buf, 0, buf.length);
@@ -77,7 +76,15 @@
     if (csq == null) {
       csq = "null";
     }
-    checkPositionIndexes(start, end, csq.length());
+    /*
+     * Ensure we throw StringIndexOutOfBoundsException as String#subsequence method depends on
+     * JavaScript's string#substr() which has different behaviour than original Java method.
+     *
+     * JavaScript behaviour of substr(start, length):
+     * "gwt".substr(0, -1) returns ""
+     * "gwt".substr(-1, 1) returns "t" -> Calculates substring from end.
+     */
+    checkCriticalStringBounds(start, end, csq.length());
     write(csq.subSequence(start, end).toString());
     return this;
   }
diff --git a/user/test/com/google/gwt/emultest/java/io/OutputStreamWriterTest.java b/user/test/com/google/gwt/emultest/java/io/OutputStreamWriterTest.java
index 49ae2b2..a9b9e25 100644
--- a/user/test/com/google/gwt/emultest/java/io/OutputStreamWriterTest.java
+++ b/user/test/com/google/gwt/emultest/java/io/OutputStreamWriterTest.java
@@ -78,11 +78,13 @@
 
   public void testWriteUnicodeChar() throws IOException {
     writer.write(UNICODE_STRING, 0, UNICODE_STRING.length());
+    writer.close();
     assertTrue(Arrays.equals(UNICODE_STRING.getBytes(encodingUTF8Charset), baos.toByteArray()));
   }
 
   public void testWriteASCIIChar() throws IOException {
     writer.write(ASCII_CHAR_ARRAY, 0, ASCII_CHAR_ARRAY.length);
+    writer.close();
     assertTrue(
         Arrays.equals(
             new String(ASCII_CHAR_ARRAY).getBytes(encodingUTF8Charset), baos.toByteArray()));
diff --git a/user/test/com/google/gwt/emultest/java/io/WriterTest.java b/user/test/com/google/gwt/emultest/java/io/WriterTest.java
index 7df1001..adbae10 100644
--- a/user/test/com/google/gwt/emultest/java/io/WriterTest.java
+++ b/user/test/com/google/gwt/emultest/java/io/WriterTest.java
@@ -122,8 +122,8 @@
     final CharSequence csq = "hola";
     try {
       writer.append(csq, -1, 2);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IndexOutOfBoundsException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }
 
@@ -131,8 +131,8 @@
     final CharSequence csq = "hola";
     try {
       writer.append(csq, 0, -1);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IllegalArgumentException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }
 
@@ -140,8 +140,8 @@
     final CharSequence csq = "hola";
     try {
       writer.append(csq, 2, 1);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IllegalArgumentException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }
 
@@ -213,8 +213,8 @@
     final String str = "hola";
     try {
       writer.append(str, -1, 2);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IndexOutOfBoundsException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }
 
@@ -222,8 +222,8 @@
     final String str = "hola";
     try {
       writer.append(str, 0, -1);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IllegalArgumentException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }
 
@@ -231,8 +231,8 @@
     final String str = "hola";
     try {
       writer.append(str, 2, 1);
-      fail("should have thrown IndexOutOfBoundsException");
-    } catch (IllegalArgumentException expected) {
+      fail("should have thrown StringIndexOutOfBoundsException");
+    } catch (StringIndexOutOfBoundsException expected) {
     }
   }