File: MarkReset.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (195 lines) | stat: -rw-r--r-- 5,875 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*************************************************************************
/* MarkReset.java -- Tests BufferedInputStream mark/reset functionality
/*
/* Copyright (c) 1998, 2005 Free Software Foundation, Inc.
/* Written by Aaron M. Renn (arenn@urbanophile.com)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published 
/* by the Free Software Foundation, either version 2 of the License, or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
/*************************************************************************/

// Tags: JDK1.0

package gnu.testlet.java.io.BufferedInputStream;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.io.*;

public class MarkReset implements Testlet
{

public static int
marktest(InputStream ins, TestHarness harness) throws IOException
{
  BufferedInputStream bis = new BufferedInputStream(ins, 15);

  int bytes_read;  
  int total_read = 0;
  byte[] buf = new byte[12];

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bis.mark(75);
  bis.read();
  bis.read(buf);
  bis.read(buf);
  bis.read(buf);
  bis.reset();

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bis.mark(555);

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bis.reset();

  bis.read(buf);
  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  bis.mark(14);

  bis.read(buf);

  bis.reset();

  bytes_read = bis.read(buf);
  total_read += bytes_read;
  harness.debug(new String(buf, 0, bytes_read), false);

  while ((bytes_read = bis.read(buf)) != -1)
    {
      harness.debug(new String(buf, 0, bytes_read), false);
      total_read += bytes_read;
    }

  return(total_read);
}
  private static void readFully(InputStream in, int len) throws java.io.IOException
  {
    int nr;
    byte[] buf = new byte[len];
    while (len > 0) {
      if ((nr = in.read(buf, 0, len)) <= 0)
        throw new java.io.IOException("Unexpected EOF");
      len -= nr;
    }
  }

public void
test(TestHarness harness)
{
  try
    {
      harness.debug("First BufferedInputStream mark/reset series");
      harness.debug("Underlying InputStream does not support mark/reset");

      String str = "My 6th grade teacher was named Mrs. Hostetler.\n" +
        "She had a whole list of rules that you were supposed to follow\n" +
        "in class and if you broke a rule she would make you write the\n" +
        "rules out several times.  The number varied depending on what\n" +
        "rule you broke.  Since I knew I would get in trouble, I would\n" +
        "just go ahead and write out a few sets on the long school bus\n" +
        "ride home so that if had to stay in during recess and write\n" +
        "rules, five minutes later I could just tell the teacher I was\n" +
        "done so I could go outside and play kickball.\n";

      StringBufferInputStream sbis = new StringBufferInputStream(str);

      int total_read = marktest(sbis, harness);
      harness.check(total_read, str.length(), "total_read");
    }
  catch (IOException e)
    {
      harness.debug(e);
      harness.check(false);
    }
   
  try
    {
      harness.debug("Second BufferedInputStream mark/reset series");
      harness.debug("Underlying InputStream supports mark/reset");

      String str = "My first day of college was fun.  A bunch of us\n" +
         "got pretty drunk, then this guy named Rick Flake (I'm not\n" +
         "making that name up) took a piss in the bed of a Physical\n" +
         "Plant dept pickup truck.  Later on we were walking across\n" +
         "campus, saw a cop, and took off running for no reason.\n" +
         "When we got back to the dorm we found an even drunker guy\n" +
         "passed out in a shopping cart outside.\n";

      ByteArrayInputStream sbis = new ByteArrayInputStream(str.getBytes());

      int total_read = marktest(sbis, harness);
      harness.check(total_read, str.length(), "total_read");

    }
  catch (IOException e)
    {
      harness.debug(e);
      harness.check(false);
    }

  try
    {
      harness.checkPoint("Third BufferedInputStream mark/reset series");
      ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100000]);
      BufferedInputStream bis = new BufferedInputStream(bais, 2048);
      bis.mark(2048);
      readFully(bis, 2049);
      harness.check(true);
    }
  catch (IOException e)
    {
      harness.debug(e);
      harness.check(false);
    }

  try
    {
      harness.checkPoint("Forth BufferedInputStream mark/reset series");
      ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100000]);
      BufferedInputStream bis = new BufferedInputStream(bais, 2048);
      bis.mark(2050);
      readFully(bis, 2050);
      bis.mark(2052);
      readFully(bis, 2052);
      harness.check(true);
    }
  catch (IOException e)
    {
      harness.debug(e);
      harness.check(false);
    }
} // main

} // class MarkReset