KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > characterStreams


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.characterStreams
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.sql.BatchUpdateException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.CallableStatement JavaDoc;
27 import java.sql.Date JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.ResultSetMetaData JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Time JavaDoc;
34 import java.sql.Timestamp JavaDoc;
35 import java.sql.Types JavaDoc;
36
37 import java.io.*;
38
39 import org.apache.derby.tools.ij;
40 import org.apache.derby.tools.JDBCDisplayUtil;
41 import org.apache.derbyTesting.functionTests.util.TestUtil;
42
43 public class characterStreams {
44
45     private static boolean isDerbyNet;
46
47     public static void main(String JavaDoc[] args) {
48
49         isDerbyNet = TestUtil.isNetFramework();
50         if (isDerbyNet) {
51             System.out.println("SKIP TEST FOR NOW");
52             return;
53         }
54         boolean passed = true;
55         Connection JavaDoc conn = null;
56         try {
57             System.out.println("Test characterStreams starting");
58
59             // use the ij utility to read the property file and
60
// make the initial connection.
61
ij.getPropertyArg(args);
62             conn = ij.startJBMS();
63
64             conn.createStatement().executeUpdate("create table charstream(id int GENERATED ALWAYS AS IDENTITY primary key, c char(25), vc varchar(32532), lvc long varchar)");
65
66             setStreams(conn,"LONG VARCHAR");
67
68             System.out.println("run test with clob column");
69             conn.createStatement().executeUpdate("drop table charstream");
70             conn.createStatement().executeUpdate("create table charstream(id int GENERATED ALWAYS AS IDENTITY primary key, c char(25), vc varchar(32532), lvc clob)");
71             
72             setStreams(conn,"CLOB");
73
74             conn.close();
75
76         } catch (SQLException JavaDoc se) {
77             passed = false;
78             JDBCDisplayUtil.ShowSQLException(System.out, se);
79             se.printStackTrace(System.out);
80         } catch (Throwable JavaDoc e) {
81             System.out.println("FAIL -- unexpected exception caught in main():\n");
82             System.out.println(e.getMessage());
83             e.printStackTrace(System.out);
84             passed = false;
85         }
86
87         if (passed)
88             System.out.println("PASS");
89
90         System.out.println("Test characterStreams finished");
91   }
92
93     
94     
95        private static void expectedException(SQLException JavaDoc sqle) {
96
97         while (sqle != null) {
98             String JavaDoc sqlState = sqle.getSQLState();
99             if (sqlState == null) {
100                 sqlState = "<NULL>";
101             }
102             System.out.println("EXPECTED SQL Exception: (" + sqlState + ") "
103                     + sqle.getMessage());
104
105             sqle = sqle.getNextException();
106         }
107     }
108     static void setStreams(Connection JavaDoc conn,String JavaDoc colType) throws Exception JavaDoc {
109
110         ResultSet JavaDoc rs;
111
112         PreparedStatement JavaDoc psi = conn.prepareStatement("insert into charstream(c, vc, lvc) values(?,?,?)");
113         PreparedStatement JavaDoc psq = conn.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");
114
115         psi.setString(1, null);
116         psi.setString(2, null);
117         psi.setString(3, null);
118
119         // test setAsciiStream into CHAR
120
System.out.println("\nTest setAsciiStream into CHAR");
121         int maxid = getMaxId(conn);
122         setAscii(psi, 1);
123         psq.setInt(1, maxid);
124         rs = psq.executeQuery();
125         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
126     
127
128         // Show results as various streams
129
PreparedStatement JavaDoc psStreams = conn.prepareStatement("SELECT id, c, vc, lvc from charstream where id > ? order by 1");
130         psStreams.setInt(1, maxid);
131         rs = psStreams.executeQuery();
132         showResultsAsciiStream(rs);
133         rs = psStreams.executeQuery();
134         showResultsCharacterStream(rs);
135         rs = psStreams.executeQuery();
136         showResultsCharacterStreamBlock(rs);
137
138         psi.setString(1, null);
139         psi.setString(2, null);
140         psi.setString(3, null);
141         // test setAsciiStream into VARCHAR
142
System.out.println("\nTest setAsciiStream into VARCHAR");
143         maxid = getMaxId(conn);
144         setAscii(psi, 2);
145         psq.setInt(1, maxid);
146         rs = psq.executeQuery();
147         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
148         psStreams.setInt(1, maxid);
149         rs = psStreams.executeQuery();
150         showResultsAsciiStream(rs);
151         rs = psStreams.executeQuery();
152         showResultsCharacterStream(rs);
153         rs = psStreams.executeQuery();
154         showResultsCharacterStreamBlock(rs);
155
156         psi.setString(1, null);
157         psi.setString(2, null);
158         psi.setString(3, null);
159         // test setAsciiStream into lvc column
160
System.out.println("\nTest setAsciiStream into "+colType);
161         maxid = getMaxId(conn);
162         setAscii(psi, 3);
163         psq.setInt(1, maxid);
164         rs = psq.executeQuery();
165         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
166         psStreams.setInt(1, maxid);
167         rs = psStreams.executeQuery();
168         showResultsAsciiStream(rs);
169         rs = psStreams.executeQuery();
170         showResultsCharacterStream(rs);
171         rs = psStreams.executeQuery();
172         showResultsCharacterStreamBlock(rs);
173
174         psi.setString(1, null);
175         psi.setString(2, null);
176         psi.setString(3, null);
177
178         // test setCharacterStream into CHAR
179
System.out.println("\nTest setCharacterStream into CHAR");
180         maxid = getMaxId(conn);
181         setCharacter(psi, 1);
182         psq.setInt(1, maxid);
183         rs = psq.executeQuery();
184         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
185
186         psi.setString(1, null);
187         psi.setString(2, null);
188         psi.setString(3, null);
189
190         // test setCharacterStream into VARCHAR
191
System.out.println("\nTest setCharacterStream into VARCHAR");
192         maxid = getMaxId(conn);
193         setCharacter(psi, 2);
194         psq.setInt(1, maxid);
195         rs = psq.executeQuery();
196         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
197
198         psi.setString(1, null);
199         psi.setString(2, null);
200         psi.setString(3, null);
201
202         // test setCharacterStream into LONG VARCHAR
203
System.out.println("\nTest setCharacterStream into "+colType);
204         maxid = getMaxId(conn);
205         setCharacter(psi, 3);
206         psq.setInt(1, maxid);
207         rs = psq.executeQuery();
208         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
209
210         // now insert long values using streams and check them programatically.
211
PreparedStatement JavaDoc psDel = conn.prepareStatement("DELETE FROM charstream");
212         PreparedStatement JavaDoc psq2 = conn.prepareStatement("select c, vc, lvc from charstream");
213
214         // now insert long values using streams and check them programatically.
215
System.out.println("setAsciiStream(LONG ASCII STREAMS)");
216         checkAsciiStreams(psDel, psi, psq2, 18, 104, 67,colType);
217         checkAsciiStreams(psDel, psi, psq2, 25, 16732, 14563,colType);
218         checkAsciiStreams(psDel, psi, psq2, 1, 32433, 32673,colType);
219         checkAsciiStreams(psDel, psi, psq2, 0, 32532, 32700,colType);
220
221         System.out.println("setCharacterStream(LONG CHARACTER STREAMS WITH UNICODE)");
222         checkCharacterStreams(psDel, psi, psq2, 14, 93, 55,colType);
223         checkCharacterStreams(psDel, psi, psq2, 25, 19332, 18733,colType);
224         checkCharacterStreams(psDel, psi, psq2, 1, 32433, 32673,colType);
225         checkCharacterStreams(psDel, psi, psq2, 0, 32532, 32700,colType);
226         
227         rs.close();
228     }
229
230     private static int getMaxId(Connection JavaDoc conn) throws SQLException JavaDoc {
231
232         ResultSet JavaDoc rs = conn.createStatement().executeQuery("select max(id) from charstream");
233         rs.next();
234         int maxid = rs.getInt(1);
235         rs.close();
236         return maxid;
237     }
238
239     private static void setAscii(PreparedStatement JavaDoc ps, int targetCol) throws Exception JavaDoc {
240
241         // correct byte count
242
System.out.println("CORRECT NUMBER OF BYTES IN STREAM");
243         ps.setAsciiStream(targetCol, new java.io.ByteArrayInputStream JavaDoc("Lieberman ran with Gore".getBytes("US-ASCII")), 23);
244         ps.executeUpdate();
245
246         // less bytes than stream contains. JDBC 3.0 indicates it should throw an exception
247
// (in Tutorial & reference book)
248
System.out.println("MORE BYTES IN STREAM THAN PASSED IN VALUE");
249         try {
250             ps.setAsciiStream(targetCol, new java.io.ByteArrayInputStream JavaDoc("against Republicans George W. Bush ".getBytes("US-ASCII")), 19);
251             ps.executeUpdate();
252             System.out.println("FAIL - MORE BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - ACCEPTED");
253         } catch (SQLException JavaDoc sqle) {
254             System.out.println("MORE BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - REJECTED ");
255             expectedException(sqle);
256         }
257
258         // more bytes than the stream contains
259
// JDBC 3.0 changed to indicate an exception should be thrown. (in Tutorial & reference book)
260
System.out.println("LESS BYTES IN STREAM THAN PASSED IN VALUE");
261         try {
262             ps.setAsciiStream(targetCol, new java.io.ByteArrayInputStream JavaDoc("and Dick Cheney.".getBytes("US-ASCII")), 17);
263             ps.executeUpdate();
264             System.out.println("FAIL - LESS BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - ACCEPTED");
265         } catch (SQLException JavaDoc sqle) {
266             System.out.println("LESS BYTES IN ASCII STREAM THAN SPECIFIED LENGTH - REJECTED ");
267             expectedException(sqle);
268         }
269
270         // null
271
System.out.println("NULL ASCII STREAM");
272         ps.setAsciiStream(targetCol, null, 1);
273         ps.executeUpdate();
274
275     }
276
277     private static void setCharacter(PreparedStatement JavaDoc ps, int targetCol) throws Exception JavaDoc {
278
279         // correct character count
280
ps.setCharacterStream(targetCol, new java.io.StringReader JavaDoc("A Mississippi Republican"), 24);
281         ps.executeUpdate();
282
283         ps.setCharacterStream(targetCol, new java.io.StringReader JavaDoc("Lott has apologized"), 19);
284         ps.executeUpdate();
285
286         // less bytes than stream contains.
287
try {
288             ps.setCharacterStream(targetCol, new java.io.StringReader JavaDoc("for comments he made at"), 20);
289             ps.executeUpdate();
290             System.out.println("FAIL - MORE CHARACTERS IN READER THAN SPECIFIED LENGTH - ACCEPTED");
291         } catch (SQLException JavaDoc sqle) {
292             System.out.println("MORE CHARACTERS IN READER THAN SPECIFIED LENGTH - REJECTED ");
293             expectedException(sqle);
294         }
295
296         // more bytes than the stream contains,
297
// JDBC 3.0 changed to indicate an exception should be thrown.
298
try {
299             ps.setCharacterStream(targetCol, new java.io.StringReader JavaDoc("a birthday party"), 17);
300             ps.executeUpdate();
301             System.out.println("FAIL - LESS CHARACTERS IN READER THAN SPECIFIED LENGTH - ACCEPTED");
302         } catch (SQLException JavaDoc sqle) {
303             System.out.println("LESS CHARACTERS IN READER STREAM THAN SPECIFIED LENGTH - REJECTED ");
304             expectedException(sqle);
305         }
306
307         // null
308
ps.setCharacterStream(targetCol, null, 1);
309         ps.executeUpdate();
310
311     }
312
313     private static void showResultsAsciiStream(ResultSet JavaDoc rs) throws SQLException JavaDoc, java.io.IOException JavaDoc {
314
315         System.out.println("Results from ASCII stream");
316         while (rs.next()) {
317             System.out.print(rs.getInt(1));
318             System.out.print(",");
319             showAsciiStream(rs.getAsciiStream(2));
320             System.out.print(",");
321             showAsciiStream(rs.getAsciiStream(3));
322             System.out.print(",");
323             showAsciiStream(rs.getAsciiStream(4));
324             System.out.println("");
325         }
326         rs.close();
327     }
328
329     private static void showAsciiStream(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc {
330
331         if (is == null) {
332             System.out.print("<NULL>");
333             return;
334         }
335
336         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
337         for (;;) {
338
339             int b = is.read();
340             if (b == -1)
341                 break;
342
343             sb.append((char) b);
344         }
345
346         System.out.print(sb.toString());
347     }
348     private static void showResultsCharacterStream(ResultSet JavaDoc rs) throws SQLException JavaDoc, java.io.IOException JavaDoc {
349
350         System.out.println("Results from Character Stream (read char) stream");
351         while (rs.next()) {
352             System.out.print(rs.getInt(1));
353             System.out.print(",");
354             showCharacterStream(rs.getCharacterStream(2));
355             System.out.print(",");
356             showCharacterStream(rs.getCharacterStream(3));
357             System.out.print(",");
358             showCharacterStream(rs.getCharacterStream(4));
359             System.out.println("");
360         }
361         rs.close();
362     }
363     private static void showResultsCharacterStreamBlock(ResultSet JavaDoc rs) throws SQLException JavaDoc, java.io.IOException JavaDoc {
364
365         System.out.println("Results from Character Stream (read block) stream");
366         while (rs.next()) {
367             System.out.print(rs.getInt(1));
368             System.out.print(",");
369             showCharacterStreamBlock(rs.getCharacterStream(2));
370             System.out.print(",");
371             showCharacterStreamBlock(rs.getCharacterStream(3));
372             System.out.print(",");
373             showCharacterStreamBlock(rs.getCharacterStream(4));
374             System.out.println("");
375         }
376         rs.close();
377     }
378     private static void showCharacterStream(java.io.Reader JavaDoc r) throws java.io.IOException JavaDoc {
379
380         if (r == null) {
381             System.out.print("<NULL>");
382             return;
383         }
384
385         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
386         for (;;) {
387
388             int b = r.read();
389             if (b == -1)
390                 break;
391
392             sb.append((char) b);
393         }
394
395         System.out.print(sb.toString());
396     }
397     private static void showCharacterStreamBlock(java.io.Reader JavaDoc r) throws java.io.IOException JavaDoc {
398
399         if (r == null) {
400             System.out.print("<NULL>");
401             return;
402         }
403
404         char[] buf = new char[2];
405
406         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
407         for (;;) {
408
409             int read = r.read(buf, 0, buf.length);
410             if (read == -1)
411                 break;
412
413             sb.append(buf, 0, read);
414         }
415
416         System.out.print(sb.toString());
417     }
418
419     private static void checkAsciiStreams(PreparedStatement JavaDoc psDel, PreparedStatement JavaDoc psi, PreparedStatement JavaDoc psq2,
420                 int cl, int vcl, int lvcl,String JavaDoc colType)
421                 throws SQLException JavaDoc, java.io.IOException JavaDoc {
422
423         psDel.executeUpdate();
424
425         // now insert long values using streams and check them programatically.
426
psi.setAsciiStream(1, new c3AsciiStream(cl), cl);
427         psi.setAsciiStream(2, new c3AsciiStream(vcl), vcl);
428         psi.setAsciiStream(3, new c3AsciiStream(lvcl), lvcl);
429         psi.executeUpdate();
430
431         ResultSet JavaDoc rs = psq2.executeQuery();
432         rs.next();
433
434         InputStream is = rs.getAsciiStream(1);
435         System.out.print("AS-CHAR-" + cl + " ");
436         c3AsciiStream.check(is, cl, 25);
437         System.out.println("DONE");
438
439         is = rs.getAsciiStream(2);
440         System.out.print("AS-VARCHAR-" + vcl + " ");
441         c3AsciiStream.check(is, vcl, -1);
442         System.out.println("DONE");
443
444         is = rs.getAsciiStream(3);
445         System.out.print("AS-"+colType+"-" + lvcl + " ");
446         c3AsciiStream.check(is, lvcl, -1);
447         System.out.println("DONE");
448
449         rs.close();
450         
451         rs = psq2.executeQuery();
452         rs.next();
453
454         Reader r = rs.getCharacterStream(1);
455         System.out.print("CS-CHAR-" + cl + " ");
456         c3AsciiStream.check(r, cl, 25);
457         System.out.println("DONE");
458
459         r = rs.getCharacterStream(2);
460         System.out.print("CS-VARCHAR-" + vcl + " ");
461         c3AsciiStream.check(r, vcl, -1);
462         System.out.println("DONE");
463
464         r = rs.getCharacterStream(3);
465         System.out.print("CS-"+colType+"-" + lvcl + " ");
466         c3AsciiStream.check(r, lvcl, -1);
467         System.out.println("DONE");
468
469         rs.close();
470
471         // and check as Strings
472

473         rs = psq2.executeQuery();
474         rs.next();
475
476         r = new java.io.StringReader JavaDoc(rs.getString(1));
477         System.out.print("ST-CHAR-" + cl + " ");
478         c3AsciiStream.check(r, cl, 25);
479         System.out.println("DONE");
480
481         r = new java.io.StringReader JavaDoc(rs.getString(2));
482         System.out.print("ST-VARCHAR-" + vcl + " ");
483         c3AsciiStream.check(r, vcl, -1);
484         System.out.println("DONE");
485
486         r = new java.io.StringReader JavaDoc(rs.getString(3));
487         System.out.print("ST-"+colType+"-" + lvcl + " ");
488         c3AsciiStream.check(r, lvcl, -1);
489         System.out.println("DONE");
490
491         rs.close();
492         }
493
494     private static void checkCharacterStreams(PreparedStatement JavaDoc psDel, PreparedStatement JavaDoc psi, PreparedStatement JavaDoc psq2,
495                 int cl, int vcl, int lvcl,String JavaDoc colType)
496                 throws SQLException JavaDoc, java.io.IOException JavaDoc {
497
498         psDel.executeUpdate();
499
500         psi.setCharacterStream(1, new c3Reader(cl), cl);
501         psi.setCharacterStream(2, new c3Reader(vcl), vcl);
502         psi.setCharacterStream(3, new c3Reader(lvcl), lvcl);
503         psi.executeUpdate();
504
505         ResultSet JavaDoc rs = psq2.executeQuery();
506         rs.next();
507
508         InputStream is = rs.getAsciiStream(1);
509         System.out.print("AS-CHAR-" + cl + " ");
510         c3Reader.check(is, cl, 25);
511         System.out.println("DONE");
512
513         is = rs.getAsciiStream(2);
514         System.out.print("AS-VARCHAR-" + vcl + " ");
515         c3Reader.check(is, vcl, -1);
516         System.out.println("DONE");
517
518         is = rs.getAsciiStream(3);
519         System.out.print("AS-"+colType+"-" + lvcl + " ");
520         c3Reader.check(is, lvcl, -1);
521         System.out.println("DONE");
522
523         rs.close();
524         
525         rs = psq2.executeQuery();
526         rs.next();
527
528         Reader r = rs.getCharacterStream(1);
529         System.out.print("CS-CHAR-" + cl + " ");
530         c3Reader.check(r, cl, 25);
531         System.out.println("DONE");
532
533         r = rs.getCharacterStream(2);
534         System.out.print("CS-VARCHAR-" + vcl + " ");
535         c3Reader.check(r, vcl, -1);
536         System.out.println("DONE");
537
538         r = rs.getCharacterStream(3);
539         System.out.print("CS-"+colType+"-" + lvcl + " ");
540         c3Reader.check(r, lvcl, -1);
541         System.out.println("DONE");
542
543         rs.close();
544
545         // check converting them into Strings work
546
rs = psq2.executeQuery();
547         rs.next();
548
549         String JavaDoc suv = rs.getString(1);
550         r = new java.io.StringReader JavaDoc(suv);
551         System.out.print("ST-CHAR-" + cl + " ");
552         c3Reader.check(r, cl, 25);
553         System.out.println("DONE");
554
555         suv = rs.getString(2);
556         r = new java.io.StringReader JavaDoc(suv);
557         System.out.print("ST-VARCHAR-" + vcl + " ");
558         c3Reader.check(r, vcl, -1);
559         System.out.println("DONE");
560
561         suv = rs.getString(3);
562         r = new java.io.StringReader JavaDoc(suv);
563         System.out.print("ST-"+colType+"-" + lvcl + " ");
564         c3Reader.check(r, lvcl, -1);
565         System.out.println("DONE");
566
567         rs.close();
568
569         }
570 }
571
572 class c3AsciiStream extends java.io.InputStream JavaDoc {
573
574     private final int size;
575     private int count;
576     c3AsciiStream(int size) {
577         this.size = size;
578     }
579     public int read(byte[] buf, int off, int length) {
580         if (count >= size)
581             return -1;
582
583         if (length > (size - count))
584             length = (size - count);
585
586         // ensure the readers don't always get a full buffer,
587
// makes sure they are not assuming the buffer will be filled.
588

589         if (length > 20)
590             length -= 17;
591
592         for (int i = 0; i < length ; i++) {
593             buf[off + i] = (byte) count++;
594         }
595
596         return length;
597     }
598
599     private byte[] rd = new byte[1];
600     public int read() {
601
602         int read = read(rd, 0, 1);
603         if (read == -1)
604             return -1;
605         return rd[0] & 0xFF;
606     }
607
608     public void close() {
609     }
610
611     static void check(InputStream is, int length, int fixedLen) throws java.io.IOException JavaDoc {
612
613         InputStream orig = new c3AsciiStream(length);
614
615         int count = 0;
616         for (;;) {
617
618             int o = orig == null ? (count == fixedLen ? -2 : 0x20) : orig.read();
619             int c = is.read();
620             if (o == -1) {
621                 orig = null;
622                 if (fixedLen != -1 && fixedLen != length)
623                     o = ' ';
624             }
625             if (o == -2)
626                 o = -1;
627
628             if ((byte) o != (byte) c) {
629                 System.out.print("F@" + count +"("+((byte)o)+","+((byte)c)+")");
630             }
631             if (orig == null) {
632                 if (fixedLen == -1)
633                     break;
634             }
635
636             if (c == -1 && fixedLen != -1)
637                 break;
638             
639             count++;
640         }
641         if (fixedLen != -1)
642             length = fixedLen;
643
644         if (count != length) {
645             System.out.print("FAIL-LEN" + count + " expected " + length);
646         }
647         is.close();
648     }
649     static void check(Reader r, int length, int fixedLen) throws java.io.IOException JavaDoc {
650
651         InputStream orig = new c3AsciiStream(length);
652
653         int count = 0;
654         for (;;) {
655
656             int o = orig == null ? (count == fixedLen ? -2 : 0x20) : orig.read();
657             int c = r.read();
658             if (o == -1) {
659                 orig = null;
660                 if (fixedLen != -1 && fixedLen != length)
661                     o = ' ';
662             }
663             if (o == -2)
664                 o = -1;
665
666             if (o != c) {
667                 System.out.print("F@" + count +"("+o+","+c+")");
668             }
669             if (orig == null) {
670                 if (fixedLen == -1)
671                     break;
672             }
673
674             if (c == -1 && fixedLen != -1)
675                 break;
676             
677             count++;
678         }
679         if (fixedLen != -1)
680             length = fixedLen;
681
682         if (count != length) {
683             System.out.print("FAIL-LEN" + count + " expected " + length);
684         }
685         r.close();
686     }
687 }
688
689 class c3Reader extends java.io.Reader JavaDoc {
690
691     private final int size;
692     private int count;
693     c3Reader(int size) {
694         this.size = size;
695     }
696     public int read(char[] buf, int off, int length) {
697         if (count >= size)
698             return -1;
699
700         if (length > (size - count))
701             length = (size - count);
702
703         // ensure the readers don't always get a full buffer,
704
// makes sure they are not assuming the buffer will be filled.
705

706         if (length > 20)
707             length -= 17;
708
709         for (int i = 0; i < length ; i++) {
710             char c;
711             switch (count % 3) {
712             case 0:
713                 c = (char) (count & 0x7F); // one byte UTF8
714
break;
715             case 1:
716                 c = (char) ((count + 0x7F) & 0x07FF); // two byte UTF8
717
break;
718             default:
719             case 2:
720                 c = (char) (count + 0x07FF); // three byte UTF8
721
break;
722
723             }
724             buf[off + i] = c;
725             count++;
726         }
727         return length;
728     }
729
730     public void close() {
731     }
732     static void check(InputStream is, int length, int fixedLen) throws java.io.IOException JavaDoc {
733
734         Reader orig = new c3Reader(length);
735
736         int count = 0;
737         for (;;) {
738
739             int o = orig == null ? (count == fixedLen ? -2 : 0x20) : orig.read();
740             int c = is.read();
741             if (o == -1) {
742                 orig = null;
743                 if (fixedLen != -1 && fixedLen != length)
744                     o = ' ';
745             }
746             if (o == -2)
747                 o = -1;
748
749             if (o != -1) {
750                 if (o <= 255)
751                     o = o & 0xFF; // convert to single byte exended ASCII
752
else
753                     o = '?'; // out of range character.
754
}
755
756             if (o != c) {
757                 System.out.print("F@" + count +"("+o+","+c+")");
758             }
759             if (orig == null) {
760                 if (fixedLen == -1)
761                     break;
762             }
763
764             if (c == -1 && fixedLen != -1)
765                 break;
766             
767             count++;
768         }
769         if (fixedLen != -1)
770             length = fixedLen;
771
772         if (count != length) {
773             System.out.print("FAIL-LEN" + count + " expected " + length);
774         }
775         is.close();
776     }
777     static void check(Reader r, int length, int fixedLen) throws java.io.IOException JavaDoc {
778
779         Reader orig = new c3Reader(length);
780
781         int count = 0;
782         for (;;) {
783
784             int o = orig == null ? (count == fixedLen ? -2 : 0x20) : orig.read();
785             int c = r.read();
786             if (o == -1) {
787                 orig = null;
788                 if (fixedLen != -1 && fixedLen != length)
789                     o = ' ';
790             }
791             if (o == -2)
792                 o = -1;
793
794             if (o != c) {
795                 System.out.print("F@" + count +"("+o+","+c+")");
796             }
797             if (orig == null) {
798                 if (fixedLen == -1)
799                     break;
800             }
801
802             if (c == -1 && fixedLen != -1)
803                 break;
804             
805             count++;
806         }
807         if (fixedLen != -1)
808             length = fixedLen;
809
810         if (count != length) {
811             System.out.print("FAIL-LEN" + count + " expected " + length);
812         }
813         r.close();
814     }
815 }
816
Popular Tags