001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.characterStreams
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.BatchUpdateException;
025: import java.sql.Connection;
026: import java.sql.CallableStatement;
027: import java.sql.Date;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.ResultSetMetaData;
031: import java.sql.Statement;
032: import java.sql.SQLException;
033: import java.sql.Time;
034: import java.sql.Timestamp;
035: import java.sql.Types;
036:
037: import java.io.*;
038:
039: import org.apache.derby.tools.ij;
040: import org.apache.derby.tools.JDBCDisplayUtil;
041: import org.apache.derbyTesting.functionTests.util.TestUtil;
042:
043: public class characterStreams {
044:
045: private static boolean isDerbyNet;
046:
047: public static void main(String[] args) {
048:
049: isDerbyNet = TestUtil.isNetFramework();
050: if (isDerbyNet) {
051: System.out.println("SKIP TEST FOR NOW");
052: return;
053: }
054: boolean passed = true;
055: Connection conn = null;
056: try {
057: System.out.println("Test characterStreams starting");
058:
059: // use the ij utility to read the property file and
060: // make the initial connection.
061: ij.getPropertyArg(args);
062: conn = ij.startJBMS();
063:
064: conn
065: .createStatement()
066: .executeUpdate(
067: "create table charstream(id int GENERATED ALWAYS AS IDENTITY primary key, c char(25), vc varchar(32532), lvc long varchar)");
068:
069: setStreams(conn, "LONG VARCHAR");
070:
071: System.out.println("run test with clob column");
072: conn.createStatement().executeUpdate(
073: "drop table charstream");
074: conn
075: .createStatement()
076: .executeUpdate(
077: "create table charstream(id int GENERATED ALWAYS AS IDENTITY primary key, c char(25), vc varchar(32532), lvc clob)");
078:
079: setStreams(conn, "CLOB");
080:
081: conn.close();
082:
083: } catch (SQLException se) {
084: passed = false;
085: JDBCDisplayUtil.ShowSQLException(System.out, se);
086: se.printStackTrace(System.out);
087: } catch (Throwable e) {
088: System.out
089: .println("FAIL -- unexpected exception caught in main():\n");
090: System.out.println(e.getMessage());
091: e.printStackTrace(System.out);
092: passed = false;
093: }
094:
095: if (passed)
096: System.out.println("PASS");
097:
098: System.out.println("Test characterStreams finished");
099: }
100:
101: private static void expectedException(SQLException sqle) {
102:
103: while (sqle != null) {
104: String sqlState = sqle.getSQLState();
105: if (sqlState == null) {
106: sqlState = "<NULL>";
107: }
108: System.out.println("EXPECTED SQL Exception: (" + sqlState
109: + ") " + sqle.getMessage());
110:
111: sqle = sqle.getNextException();
112: }
113: }
114:
115: static void setStreams(Connection conn, String colType)
116: throws Exception {
117:
118: ResultSet rs;
119:
120: PreparedStatement psi = conn
121: .prepareStatement("insert into charstream(c, vc, lvc) values(?,?,?)");
122: PreparedStatement psq = conn
123: .prepareStatement("select id, c, {fn length(c)} AS CLEN, cast (vc as varchar(25)) AS VC, {fn length(vc)} AS VCLEN, cast (lvc as varchar(25)) AS LVC, {fn length(lvc)} AS LVCLEN from charstream where id > ? order by 1");
124:
125: psi.setString(1, null);
126: psi.setString(2, null);
127: psi.setString(3, null);
128:
129: // test setAsciiStream into CHAR
130: System.out.println("\nTest setAsciiStream into CHAR");
131: int maxid = getMaxId(conn);
132: setAscii(psi, 1);
133: psq.setInt(1, maxid);
134: rs = psq.executeQuery();
135: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
136:
137: // Show results as various streams
138: PreparedStatement psStreams = conn
139: .prepareStatement("SELECT id, c, vc, lvc from charstream where id > ? order by 1");
140: psStreams.setInt(1, maxid);
141: rs = psStreams.executeQuery();
142: showResultsAsciiStream(rs);
143: rs = psStreams.executeQuery();
144: showResultsCharacterStream(rs);
145: rs = psStreams.executeQuery();
146: showResultsCharacterStreamBlock(rs);
147:
148: psi.setString(1, null);
149: psi.setString(2, null);
150: psi.setString(3, null);
151: // test setAsciiStream into VARCHAR
152: System.out.println("\nTest setAsciiStream into VARCHAR");
153: maxid = getMaxId(conn);
154: setAscii(psi, 2);
155: psq.setInt(1, maxid);
156: rs = psq.executeQuery();
157: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
158: psStreams.setInt(1, maxid);
159: rs = psStreams.executeQuery();
160: showResultsAsciiStream(rs);
161: rs = psStreams.executeQuery();
162: showResultsCharacterStream(rs);
163: rs = psStreams.executeQuery();
164: showResultsCharacterStreamBlock(rs);
165:
166: psi.setString(1, null);
167: psi.setString(2, null);
168: psi.setString(3, null);
169: // test setAsciiStream into lvc column
170: System.out.println("\nTest setAsciiStream into " + colType);
171: maxid = getMaxId(conn);
172: setAscii(psi, 3);
173: psq.setInt(1, maxid);
174: rs = psq.executeQuery();
175: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
176: psStreams.setInt(1, maxid);
177: rs = psStreams.executeQuery();
178: showResultsAsciiStream(rs);
179: rs = psStreams.executeQuery();
180: showResultsCharacterStream(rs);
181: rs = psStreams.executeQuery();
182: showResultsCharacterStreamBlock(rs);
183:
184: psi.setString(1, null);
185: psi.setString(2, null);
186: psi.setString(3, null);
187:
188: // test setCharacterStream into CHAR
189: System.out.println("\nTest setCharacterStream into CHAR");
190: maxid = getMaxId(conn);
191: setCharacter(psi, 1);
192: psq.setInt(1, maxid);
193: rs = psq.executeQuery();
194: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
195:
196: psi.setString(1, null);
197: psi.setString(2, null);
198: psi.setString(3, null);
199:
200: // test setCharacterStream into VARCHAR
201: System.out.println("\nTest setCharacterStream into VARCHAR");
202: maxid = getMaxId(conn);
203: setCharacter(psi, 2);
204: psq.setInt(1, maxid);
205: rs = psq.executeQuery();
206: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
207:
208: psi.setString(1, null);
209: psi.setString(2, null);
210: psi.setString(3, null);
211:
212: // test setCharacterStream into LONG VARCHAR
213: System.out.println("\nTest setCharacterStream into " + colType);
214: maxid = getMaxId(conn);
215: setCharacter(psi, 3);
216: psq.setInt(1, maxid);
217: rs = psq.executeQuery();
218: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
219:
220: // now insert long values using streams and check them programatically.
221: PreparedStatement psDel = conn
222: .prepareStatement("DELETE FROM charstream");
223: PreparedStatement psq2 = conn
224: .prepareStatement("select c, vc, lvc from charstream");
225:
226: // now insert long values using streams and check them programatically.
227: System.out.println("setAsciiStream(LONG ASCII STREAMS)");
228: checkAsciiStreams(psDel, psi, psq2, 18, 104, 67, colType);
229: checkAsciiStreams(psDel, psi, psq2, 25, 16732, 14563, colType);
230: checkAsciiStreams(psDel, psi, psq2, 1, 32433, 32673, colType);
231: checkAsciiStreams(psDel, psi, psq2, 0, 32532, 32700, colType);
232:
233: System.out
234: .println("setCharacterStream(LONG CHARACTER STREAMS WITH UNICODE)");
235: checkCharacterStreams(psDel, psi, psq2, 14, 93, 55, colType);
236: checkCharacterStreams(psDel, psi, psq2, 25, 19332, 18733,
237: colType);
238: checkCharacterStreams(psDel, psi, psq2, 1, 32433, 32673,
239: colType);
240: checkCharacterStreams(psDel, psi, psq2, 0, 32532, 32700,
241: colType);
242:
243: rs.close();
244: }
245:
246: private static int getMaxId(Connection conn) throws SQLException {
247:
248: ResultSet rs = conn.createStatement().executeQuery(
249: "select max(id) from charstream");
250: rs.next();
251: int maxid = rs.getInt(1);
252: rs.close();
253: return maxid;
254: }
255:
256: private static void setAscii(PreparedStatement ps, int targetCol)
257: throws Exception {
258:
259: // correct byte count
260: System.out.println("CORRECT NUMBER OF BYTES IN STREAM");
261: ps.setAsciiStream(targetCol, new java.io.ByteArrayInputStream(
262: "Lieberman ran with Gore".getBytes("US-ASCII")), 23);
263: ps.executeUpdate();
264:
265: // less bytes than stream contains. JDBC 3.0 indicates it should throw an exception
266: // (in Tutorial & reference book)
267: System.out.println("MORE BYTES IN STREAM THAN PASSED IN VALUE");
268: try {
269: ps.setAsciiStream(targetCol,
270: new java.io.ByteArrayInputStream(
271: "against Republicans George W. Bush "
272: .getBytes("US-ASCII")), 19);
273: ps.executeUpdate();
274: System.out
275: .println("FAIL - MORE BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - ACCEPTED");
276: } catch (SQLException sqle) {
277: System.out
278: .println("MORE BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - REJECTED ");
279: expectedException(sqle);
280: }
281:
282: // more bytes than the stream contains
283: // JDBC 3.0 changed to indicate an exception should be thrown. (in Tutorial & reference book)
284: System.out.println("LESS BYTES IN STREAM THAN PASSED IN VALUE");
285: try {
286: ps.setAsciiStream(targetCol,
287: new java.io.ByteArrayInputStream("and Dick Cheney."
288: .getBytes("US-ASCII")), 17);
289: ps.executeUpdate();
290: System.out
291: .println("FAIL - LESS BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - ACCEPTED");
292: } catch (SQLException sqle) {
293: System.out
294: .println("LESS BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - REJECTED ");
295: expectedException(sqle);
296: }
297:
298: // null
299: System.out.println("NULL ASCII STREAM");
300: ps.setAsciiStream(targetCol, null, 1);
301: ps.executeUpdate();
302:
303: }
304:
305: private static void setCharacter(PreparedStatement ps, int targetCol)
306: throws Exception {
307:
308: // correct character count
309: ps.setCharacterStream(targetCol, new java.io.StringReader(
310: "A Mississippi Republican"), 24);
311: ps.executeUpdate();
312:
313: ps.setCharacterStream(targetCol, new java.io.StringReader(
314: "Lott has apologized"), 19);
315: ps.executeUpdate();
316:
317: // less bytes than stream contains.
318: try {
319: ps.setCharacterStream(targetCol, new java.io.StringReader(
320: "for comments he made at"), 20);
321: ps.executeUpdate();
322: System.out
323: .println("FAIL - MORE CHARACTERS IN READER THAN SPECIFIED LENGTH - ACCEPTED");
324: } catch (SQLException sqle) {
325: System.out
326: .println("MORE CHARACTERS IN READER THAN SPECIFIED LENGTH - REJECTED ");
327: expectedException(sqle);
328: }
329:
330: // more bytes than the stream contains,
331: // JDBC 3.0 changed to indicate an exception should be thrown.
332: try {
333: ps.setCharacterStream(targetCol, new java.io.StringReader(
334: "a birthday party"), 17);
335: ps.executeUpdate();
336: System.out
337: .println("FAIL - LESS CHARACTERS IN READER THAN SPECIFIED LENGTH - ACCEPTED");
338: } catch (SQLException sqle) {
339: System.out
340: .println("LESS CHARACTERS IN READER STREAM THAN SPECIFIED LENGTH - REJECTED ");
341: expectedException(sqle);
342: }
343:
344: // null
345: ps.setCharacterStream(targetCol, null, 1);
346: ps.executeUpdate();
347:
348: }
349:
350: private static void showResultsAsciiStream(ResultSet rs)
351: throws SQLException, java.io.IOException {
352:
353: System.out.println("Results from ASCII stream");
354: while (rs.next()) {
355: System.out.print(rs.getInt(1));
356: System.out.print(",");
357: showAsciiStream(rs.getAsciiStream(2));
358: System.out.print(",");
359: showAsciiStream(rs.getAsciiStream(3));
360: System.out.print(",");
361: showAsciiStream(rs.getAsciiStream(4));
362: System.out.println("");
363: }
364: rs.close();
365: }
366:
367: private static void showAsciiStream(java.io.InputStream is)
368: throws java.io.IOException {
369:
370: if (is == null) {
371: System.out.print("<NULL>");
372: return;
373: }
374:
375: StringBuffer sb = new StringBuffer();
376: for (;;) {
377:
378: int b = is.read();
379: if (b == -1)
380: break;
381:
382: sb.append((char) b);
383: }
384:
385: System.out.print(sb.toString());
386: }
387:
388: private static void showResultsCharacterStream(ResultSet rs)
389: throws SQLException, java.io.IOException {
390:
391: System.out
392: .println("Results from Character Stream (read char) stream");
393: while (rs.next()) {
394: System.out.print(rs.getInt(1));
395: System.out.print(",");
396: showCharacterStream(rs.getCharacterStream(2));
397: System.out.print(",");
398: showCharacterStream(rs.getCharacterStream(3));
399: System.out.print(",");
400: showCharacterStream(rs.getCharacterStream(4));
401: System.out.println("");
402: }
403: rs.close();
404: }
405:
406: private static void showResultsCharacterStreamBlock(ResultSet rs)
407: throws SQLException, java.io.IOException {
408:
409: System.out
410: .println("Results from Character Stream (read block) stream");
411: while (rs.next()) {
412: System.out.print(rs.getInt(1));
413: System.out.print(",");
414: showCharacterStreamBlock(rs.getCharacterStream(2));
415: System.out.print(",");
416: showCharacterStreamBlock(rs.getCharacterStream(3));
417: System.out.print(",");
418: showCharacterStreamBlock(rs.getCharacterStream(4));
419: System.out.println("");
420: }
421: rs.close();
422: }
423:
424: private static void showCharacterStream(java.io.Reader r)
425: throws java.io.IOException {
426:
427: if (r == null) {
428: System.out.print("<NULL>");
429: return;
430: }
431:
432: StringBuffer sb = new StringBuffer();
433: for (;;) {
434:
435: int b = r.read();
436: if (b == -1)
437: break;
438:
439: sb.append((char) b);
440: }
441:
442: System.out.print(sb.toString());
443: }
444:
445: private static void showCharacterStreamBlock(java.io.Reader r)
446: throws java.io.IOException {
447:
448: if (r == null) {
449: System.out.print("<NULL>");
450: return;
451: }
452:
453: char[] buf = new char[2];
454:
455: StringBuffer sb = new StringBuffer();
456: for (;;) {
457:
458: int read = r.read(buf, 0, buf.length);
459: if (read == -1)
460: break;
461:
462: sb.append(buf, 0, read);
463: }
464:
465: System.out.print(sb.toString());
466: }
467:
468: private static void checkAsciiStreams(PreparedStatement psDel,
469: PreparedStatement psi, PreparedStatement psq2, int cl,
470: int vcl, int lvcl, String colType) throws SQLException,
471: java.io.IOException {
472:
473: psDel.executeUpdate();
474:
475: // now insert long values using streams and check them programatically.
476: psi.setAsciiStream(1, new c3AsciiStream(cl), cl);
477: psi.setAsciiStream(2, new c3AsciiStream(vcl), vcl);
478: psi.setAsciiStream(3, new c3AsciiStream(lvcl), lvcl);
479: psi.executeUpdate();
480:
481: ResultSet rs = psq2.executeQuery();
482: rs.next();
483:
484: InputStream is = rs.getAsciiStream(1);
485: System.out.print("AS-CHAR-" + cl + " ");
486: c3AsciiStream.check(is, cl, 25);
487: System.out.println("DONE");
488:
489: is = rs.getAsciiStream(2);
490: System.out.print("AS-VARCHAR-" + vcl + " ");
491: c3AsciiStream.check(is, vcl, -1);
492: System.out.println("DONE");
493:
494: is = rs.getAsciiStream(3);
495: System.out.print("AS-" + colType + "-" + lvcl + " ");
496: c3AsciiStream.check(is, lvcl, -1);
497: System.out.println("DONE");
498:
499: rs.close();
500:
501: rs = psq2.executeQuery();
502: rs.next();
503:
504: Reader r = rs.getCharacterStream(1);
505: System.out.print("CS-CHAR-" + cl + " ");
506: c3AsciiStream.check(r, cl, 25);
507: System.out.println("DONE");
508:
509: r = rs.getCharacterStream(2);
510: System.out.print("CS-VARCHAR-" + vcl + " ");
511: c3AsciiStream.check(r, vcl, -1);
512: System.out.println("DONE");
513:
514: r = rs.getCharacterStream(3);
515: System.out.print("CS-" + colType + "-" + lvcl + " ");
516: c3AsciiStream.check(r, lvcl, -1);
517: System.out.println("DONE");
518:
519: rs.close();
520:
521: // and check as Strings
522:
523: rs = psq2.executeQuery();
524: rs.next();
525:
526: r = new java.io.StringReader(rs.getString(1));
527: System.out.print("ST-CHAR-" + cl + " ");
528: c3AsciiStream.check(r, cl, 25);
529: System.out.println("DONE");
530:
531: r = new java.io.StringReader(rs.getString(2));
532: System.out.print("ST-VARCHAR-" + vcl + " ");
533: c3AsciiStream.check(r, vcl, -1);
534: System.out.println("DONE");
535:
536: r = new java.io.StringReader(rs.getString(3));
537: System.out.print("ST-" + colType + "-" + lvcl + " ");
538: c3AsciiStream.check(r, lvcl, -1);
539: System.out.println("DONE");
540:
541: rs.close();
542: }
543:
544: private static void checkCharacterStreams(PreparedStatement psDel,
545: PreparedStatement psi, PreparedStatement psq2, int cl,
546: int vcl, int lvcl, String colType) throws SQLException,
547: java.io.IOException {
548:
549: psDel.executeUpdate();
550:
551: psi.setCharacterStream(1, new c3Reader(cl), cl);
552: psi.setCharacterStream(2, new c3Reader(vcl), vcl);
553: psi.setCharacterStream(3, new c3Reader(lvcl), lvcl);
554: psi.executeUpdate();
555:
556: ResultSet rs = psq2.executeQuery();
557: rs.next();
558:
559: InputStream is = rs.getAsciiStream(1);
560: System.out.print("AS-CHAR-" + cl + " ");
561: c3Reader.check(is, cl, 25);
562: System.out.println("DONE");
563:
564: is = rs.getAsciiStream(2);
565: System.out.print("AS-VARCHAR-" + vcl + " ");
566: c3Reader.check(is, vcl, -1);
567: System.out.println("DONE");
568:
569: is = rs.getAsciiStream(3);
570: System.out.print("AS-" + colType + "-" + lvcl + " ");
571: c3Reader.check(is, lvcl, -1);
572: System.out.println("DONE");
573:
574: rs.close();
575:
576: rs = psq2.executeQuery();
577: rs.next();
578:
579: Reader r = rs.getCharacterStream(1);
580: System.out.print("CS-CHAR-" + cl + " ");
581: c3Reader.check(r, cl, 25);
582: System.out.println("DONE");
583:
584: r = rs.getCharacterStream(2);
585: System.out.print("CS-VARCHAR-" + vcl + " ");
586: c3Reader.check(r, vcl, -1);
587: System.out.println("DONE");
588:
589: r = rs.getCharacterStream(3);
590: System.out.print("CS-" + colType + "-" + lvcl + " ");
591: c3Reader.check(r, lvcl, -1);
592: System.out.println("DONE");
593:
594: rs.close();
595:
596: // check converting them into Strings work
597: rs = psq2.executeQuery();
598: rs.next();
599:
600: String suv = rs.getString(1);
601: r = new java.io.StringReader(suv);
602: System.out.print("ST-CHAR-" + cl + " ");
603: c3Reader.check(r, cl, 25);
604: System.out.println("DONE");
605:
606: suv = rs.getString(2);
607: r = new java.io.StringReader(suv);
608: System.out.print("ST-VARCHAR-" + vcl + " ");
609: c3Reader.check(r, vcl, -1);
610: System.out.println("DONE");
611:
612: suv = rs.getString(3);
613: r = new java.io.StringReader(suv);
614: System.out.print("ST-" + colType + "-" + lvcl + " ");
615: c3Reader.check(r, lvcl, -1);
616: System.out.println("DONE");
617:
618: rs.close();
619:
620: }
621: }
622:
623: class c3AsciiStream extends java.io.InputStream {
624:
625: private final int size;
626: private int count;
627:
628: c3AsciiStream(int size) {
629: this .size = size;
630: }
631:
632: public int read(byte[] buf, int off, int length) {
633: if (count >= size)
634: return -1;
635:
636: if (length > (size - count))
637: length = (size - count);
638:
639: // ensure the readers don't always get a full buffer,
640: // makes sure they are not assuming the buffer will be filled.
641:
642: if (length > 20)
643: length -= 17;
644:
645: for (int i = 0; i < length; i++) {
646: buf[off + i] = (byte) count++;
647: }
648:
649: return length;
650: }
651:
652: private byte[] rd = new byte[1];
653:
654: public int read() {
655:
656: int read = read(rd, 0, 1);
657: if (read == -1)
658: return -1;
659: return rd[0] & 0xFF;
660: }
661:
662: public void close() {
663: }
664:
665: static void check(InputStream is, int length, int fixedLen)
666: throws java.io.IOException {
667:
668: InputStream orig = new c3AsciiStream(length);
669:
670: int count = 0;
671: for (;;) {
672:
673: int o = orig == null ? (count == fixedLen ? -2 : 0x20)
674: : orig.read();
675: int c = is.read();
676: if (o == -1) {
677: orig = null;
678: if (fixedLen != -1 && fixedLen != length)
679: o = ' ';
680: }
681: if (o == -2)
682: o = -1;
683:
684: if ((byte) o != (byte) c) {
685: System.out.print("F@" + count + "(" + ((byte) o) + ","
686: + ((byte) c) + ")");
687: }
688: if (orig == null) {
689: if (fixedLen == -1)
690: break;
691: }
692:
693: if (c == -1 && fixedLen != -1)
694: break;
695:
696: count++;
697: }
698: if (fixedLen != -1)
699: length = fixedLen;
700:
701: if (count != length) {
702: System.out
703: .print("FAIL-LEN" + count + " expected " + length);
704: }
705: is.close();
706: }
707:
708: static void check(Reader r, int length, int fixedLen)
709: throws java.io.IOException {
710:
711: InputStream orig = new c3AsciiStream(length);
712:
713: int count = 0;
714: for (;;) {
715:
716: int o = orig == null ? (count == fixedLen ? -2 : 0x20)
717: : orig.read();
718: int c = r.read();
719: if (o == -1) {
720: orig = null;
721: if (fixedLen != -1 && fixedLen != length)
722: o = ' ';
723: }
724: if (o == -2)
725: o = -1;
726:
727: if (o != c) {
728: System.out
729: .print("F@" + count + "(" + o + "," + c + ")");
730: }
731: if (orig == null) {
732: if (fixedLen == -1)
733: break;
734: }
735:
736: if (c == -1 && fixedLen != -1)
737: break;
738:
739: count++;
740: }
741: if (fixedLen != -1)
742: length = fixedLen;
743:
744: if (count != length) {
745: System.out
746: .print("FAIL-LEN" + count + " expected " + length);
747: }
748: r.close();
749: }
750: }
751:
752: class c3Reader extends java.io.Reader {
753:
754: private final int size;
755: private int count;
756:
757: c3Reader(int size) {
758: this .size = size;
759: }
760:
761: public int read(char[] buf, int off, int length) {
762: if (count >= size)
763: return -1;
764:
765: if (length > (size - count))
766: length = (size - count);
767:
768: // ensure the readers don't always get a full buffer,
769: // makes sure they are not assuming the buffer will be filled.
770:
771: if (length > 20)
772: length -= 17;
773:
774: for (int i = 0; i < length; i++) {
775: char c;
776: switch (count % 3) {
777: case 0:
778: c = (char) (count & 0x7F); // one byte UTF8
779: break;
780: case 1:
781: c = (char) ((count + 0x7F) & 0x07FF); // two byte UTF8
782: break;
783: default:
784: case 2:
785: c = (char) (count + 0x07FF); // three byte UTF8
786: break;
787:
788: }
789: buf[off + i] = c;
790: count++;
791: }
792: return length;
793: }
794:
795: public void close() {
796: }
797:
798: static void check(InputStream is, int length, int fixedLen)
799: throws java.io.IOException {
800:
801: Reader orig = new c3Reader(length);
802:
803: int count = 0;
804: for (;;) {
805:
806: int o = orig == null ? (count == fixedLen ? -2 : 0x20)
807: : orig.read();
808: int c = is.read();
809: if (o == -1) {
810: orig = null;
811: if (fixedLen != -1 && fixedLen != length)
812: o = ' ';
813: }
814: if (o == -2)
815: o = -1;
816:
817: if (o != -1) {
818: if (o <= 255)
819: o = o & 0xFF; // convert to single byte exended ASCII
820: else
821: o = '?'; // out of range character.
822: }
823:
824: if (o != c) {
825: System.out
826: .print("F@" + count + "(" + o + "," + c + ")");
827: }
828: if (orig == null) {
829: if (fixedLen == -1)
830: break;
831: }
832:
833: if (c == -1 && fixedLen != -1)
834: break;
835:
836: count++;
837: }
838: if (fixedLen != -1)
839: length = fixedLen;
840:
841: if (count != length) {
842: System.out
843: .print("FAIL-LEN" + count + " expected " + length);
844: }
845: is.close();
846: }
847:
848: static void check(Reader r, int length, int fixedLen)
849: throws java.io.IOException {
850:
851: Reader orig = new c3Reader(length);
852:
853: int count = 0;
854: for (;;) {
855:
856: int o = orig == null ? (count == fixedLen ? -2 : 0x20)
857: : orig.read();
858: int c = r.read();
859: if (o == -1) {
860: orig = null;
861: if (fixedLen != -1 && fixedLen != length)
862: o = ' ';
863: }
864: if (o == -2)
865: o = -1;
866:
867: if (o != c) {
868: System.out
869: .print("F@" + count + "(" + o + "," + c + ")");
870: }
871: if (orig == null) {
872: if (fixedLen == -1)
873: break;
874: }
875:
876: if (c == -1 && fixedLen != -1)
877: break;
878:
879: count++;
880: }
881: if (fixedLen != -1)
882: length = fixedLen;
883:
884: if (count != length) {
885: System.out
886: .print("FAIL-LEN" + count + " expected " + length);
887: }
888: r.close();
889: }
890: }
|