Home | History | Annotate | Download | only in support
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package tests.support;
     19 
     20 import java.io.FileDescriptor;
     21 import java.io.FileInputStream;
     22 import java.io.FileOutputStream;
     23 import java.io.IOException;
     24 
     25 public class Support_AvailTest {
     26 
     27     public static void main(String[] args) {
     28         // This test is for:
     29 
     30         // This program accepts from stdin a
     31         // string of the form '<int1> <int2> <data>'
     32         // where data is some bytes, <int1> is the length of the whole string
     33         // and <int2> is the length in bytes of <data> (all bytes until the end
     34         // of
     35         // the input)
     36 
     37         // If the string is formatted correctly, and the available method works
     38         // the string "true" will be sent to stdout, otherwise "false"
     39         String output = "true";
     40         try {
     41             FileInputStream myin = new FileInputStream(FileDescriptor.in);
     42             StringBuffer input = new StringBuffer("");
     43 
     44             try {
     45                 Thread.sleep(500);
     46             } catch (Exception sleepException) {
     47             }
     48 
     49             int real = myin.available();
     50             int expected;
     51             int c = 0;
     52             while (true) {
     53                 c = myin.read();
     54                 if (c == ' ' || c == -1) {
     55                     break;
     56                 }
     57                 input.append((char) c);
     58             }
     59             expected = Integer.parseInt(input.toString());
     60             // Verify correct value at start of read
     61             if (real != expected) {
     62                 output = "Failed avail test1 - " + real + "!=" + expected;
     63             }
     64 
     65             c = 0;
     66             input = new StringBuffer("");
     67             while (true) {
     68                 c = myin.read();
     69                 if (c == ' ' || c == -1) {
     70                     break;
     71                 }
     72                 input.append((char) c);
     73             }
     74             expected = Integer.parseInt(input.toString());
     75             real = myin.available();
     76             // Verify value at middle of reading
     77             // This test doesn't work on Windows, at present
     78             // if(real != expected) output = "Failed avail test2 - " + real +
     79             // "!=" + expected;
     80 
     81             // Verify value at end of reading
     82             // loop to EOF, then check if available = 0
     83             // replace this:
     84             for (int i = 0; i < 5; i++) {
     85                 myin.read();
     86                 // with:
     87                 // while(myin.read() != -1);
     88             }
     89 
     90             // The current for loop reads exactly to the end
     91             // of the data, but is dependent on knowing the length of the data
     92             // sent to it, which isn't nice
     93 
     94             expected = 0;
     95             real = myin.available();
     96             if (real != 0) {
     97                 output = "Failed avail test3 - " + real + "!=" + expected;
     98             }
     99 
    100         } catch (IOException e) {
    101             output = "IOException during available() testing";
    102         }
    103 
    104         try {
    105             FileOutputStream myout = new FileOutputStream(FileDescriptor.out);
    106             myout.write(output.getBytes());
    107         } catch (IOException e) {
    108             e.printStackTrace();
    109         }
    110     }
    111 }
    112