ICU-11463 ICU4J ought to always close() resources

R=yoshito_umaoka@us.ibm.com

Review URL: https://codereview.appspot.com/199390043

X-SVN-Rev: 37034
This commit is contained in:
Fredrik Roubert 2015-02-13 20:16:22 +00:00
parent 9aadd6b1e9
commit d8b9e7ecaa
31 changed files with 571 additions and 438 deletions

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2006-2014, International Business Machines Corporation and
* Copyright (C) 2006-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
@ -211,11 +211,13 @@ class CharsetMBCS extends CharsetICU {
ByteBuffer b;
if (loader != null) {
@SuppressWarnings("resource") // Closed by getByteBufferFromInputStreamAndCloseStream().
InputStream i = ICUData.getRequiredStream(loader, resourceName);
b = ICUBinary.getByteBufferFromInputStream(i);
b = ICUBinary.getByteBufferFromInputStreamAndCloseStream(i);
} else if (!classPath.equals(ICUData.ICU_BUNDLE)) {
@SuppressWarnings("resource") // Closed by getByteBufferFromInputStreamAndCloseStream().
InputStream i = ICUData.getRequiredStream(resourceName);
b = ICUBinary.getByteBufferFromInputStream(i);
b = ICUBinary.getByteBufferFromInputStreamAndCloseStream(i);
} else {
b = ICUBinary.getRequiredData(itemName);
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2006-2014, International Business Machines Corporation and
* Copyright (C) 2006-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -415,7 +415,7 @@ final class UConverterDataReader {
/*
* UConverterDataReader(UConverterDataReader r)
{
byteBuffer = ICUBinary.getByteBufferFromInputStream(r.byteBuffer);
byteBuffer = ICUBinary.getByteBufferFromInputStreamAndCloseStream(r.byteBuffer);
unicodeVersion = r.unicodeVersion;
}
*/

View file

@ -468,15 +468,18 @@ public final class ICUBinary {
if (resourceName == null) {
resourceName = ICUData.ICU_BASE_NAME + '/' + itemPath;
}
InputStream is = ICUData.getStream(loader, resourceName, required);
if (is == null) {
return null;
}
ByteBuffer buffer = null;
try {
return getByteBufferFromInputStream(is);
@SuppressWarnings("resource") // Closed by getByteBufferFromInputStreamAndCloseStream().
InputStream is = ICUData.getStream(loader, resourceName, required);
if (is == null) {
return null;
}
buffer = getByteBufferFromInputStreamAndCloseStream(is);
} catch (IOException e) {
throw new ICUUncheckedIOException(e);
}
return buffer;
}
private static ByteBuffer getDataFromFile(String itemPath) {
@ -489,17 +492,20 @@ public final class ICUBinary {
return null;
}
@SuppressWarnings("resource") // Closing a file closes its channel.
private static ByteBuffer mapFile(File path) {
FileInputStream file;
try {
file = new FileInputStream(path);
FileChannel channel = file.getChannel();
ByteBuffer bytes = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
// Close the file and its channel; this seems to keep the ByteBuffer valid.
// If not, then we will need to return the pair of (file, bytes).
file.close();
ByteBuffer bytes = null;
try {
bytes = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
} finally {
file.close();
}
return bytes;
} catch(FileNotFoundException ignored) {
} catch (FileNotFoundException ignored) {
System.err.println(ignored);
} catch (IOException ignored) {
System.err.println(ignored);
@ -636,7 +642,7 @@ public final class ICUBinary {
* Reads the entire contents from the stream into a byte array
* and wraps it into a ByteBuffer. Closes the InputStream at the end.
*/
public static ByteBuffer getByteBufferFromInputStream(InputStream is) throws IOException {
public static ByteBuffer getByteBufferFromInputStreamAndCloseStream(InputStream is) throws IOException {
try {
// is.available() may return 0, or 1, or the total number of bytes in the stream,
// or some other number.

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2008-2010, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2008-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.impl;
@ -26,7 +26,11 @@ public class ICUConfig {
try {
InputStream is = ICUData.getStream(CONFIG_PROPS_FILE);
if (is != null) {
CONFIG_PROPS.load(is);
try {
CONFIG_PROPS.load(is);
} finally {
is.close();
}
}
} catch (MissingResourceException mre) {
// If it does not exist, ignore.

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2004-2014, International Business Machines Corporation and
* Copyright (C) 2004-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -337,11 +337,12 @@ public final class ICUResourceBundleReader {
return NULL_READER;
}
} else {
@SuppressWarnings("resource") // Closed by getByteBufferFromInputStreamAndCloseStream().
InputStream stream = ICUData.getStream(data.loader, fullName);
if (stream == null) {
return NULL_READER;
}
inBytes = ICUBinary.getByteBufferFromInputStream(stream);
inBytes = ICUBinary.getByteBufferFromInputStreamAndCloseStream(stream);
}
return new ICUResourceBundleReader(inBytes, data.baseName, data.localeID, data.loader);
} catch (IOException ex) {

View file

@ -9,6 +9,7 @@ package com.ibm.icu.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
@ -32,7 +33,9 @@ public abstract class URLHandler {
static {
Map<String, Method> h = null;
BufferedReader br = null;
try {
@SuppressWarnings("resource") // Closed by BufferedReader.
InputStream is = URLHandler.class.getResourceAsStream(PROPNAME);
if (is == null) {
ClassLoader loader = Utility.getFallbackClassLoader();
@ -41,7 +44,7 @@ public abstract class URLHandler {
if (is != null) {
Class<?>[] params = { URL.class };
BufferedReader br = new BufferedReader(new InputStreamReader(is));
br = new BufferedReader(new InputStreamReader(is));
for (String line = br.readLine(); line != null; line = br.readLine()) {
line = line.trim();
@ -84,11 +87,18 @@ public abstract class URLHandler {
}
} catch (Throwable t) {
if (DEBUG) System.err.println(t);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignored) {
}
}
}
handlers = h;
}
public static URLHandler get(URL url) {
if (url == null) {
return null;

View file

@ -1,8 +1,8 @@
/*
********************************************************************************
* Copyright (C) 2009-2011, Google, International Business Machines Corporation *
* and others. All Rights Reserved. *
********************************************************************************
*******************************************************************************
* Copyright (C) 2009-2015, Google, International Business Machines Corporation
* and others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.impl;
@ -269,7 +269,12 @@ public class UnicodeRegex implements Cloneable, Freezable<UnicodeRegex>, StringT
* @throws IOException If there were problems opening the file for input stream.
*/
public static List<String> appendLines(List<String> result, String file, String encoding) throws IOException {
return appendLines(result, new FileInputStream(file), encoding);
InputStream is = new FileInputStream(file);
try {
return appendLines(result, is, encoding);
} finally {
is.close();
}
}
/**

View file

@ -1,13 +1,14 @@
/**
*******************************************************************************
* Copyright (C) 2001-2013, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2001-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.impl.data;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -28,8 +29,8 @@ import com.ibm.icu.impl.PatternProps;
*
* @author Alan Liu
*/
public class ResourceReader {
private BufferedReader reader;
public class ResourceReader implements Closeable {
private BufferedReader reader = null;
private String resourceName;
private String encoding; // null for default encoding
private Class<?> root;
@ -229,6 +230,9 @@ public class ResourceReader {
* are large, e.g., 400k.
*/
private void _reset() throws UnsupportedEncodingException {
try {
close();
} catch (IOException e) {}
if (lineNo == 0) {
return;
}
@ -243,4 +247,16 @@ public class ResourceReader {
reader = new BufferedReader(isr);
lineNo = 0;
}
/**
* Closes the underlying reader and releases any system resources
* associated with it. If the stream is already closed then invoking
* this method has no effect.
*/
public void close() throws IOException {
if (reader != null) {
reader.close();
reader = null;
}
}
}

View file

@ -1,6 +1,6 @@
/*
******************************************************************************
* Copyright (C) 2007-2014, International Business Machines Corporation and
* Copyright (C) 2007-2015, International Business Machines Corporation and
* others. All Rights Reserved.
******************************************************************************
*/
@ -21,6 +21,7 @@ import java.util.Map;
import java.util.MissingResourceException;
import com.ibm.icu.impl.ICUData;
import com.ibm.icu.util.ICUUncheckedIOException;
/**
* A PeriodFormatterDataService that serves PeriodFormatterData objects based on
@ -70,6 +71,11 @@ public class ResourceBasedPeriodFormatterDataService extends
} catch (IOException e) {
throw new IllegalStateException("IO Error reading " + PATH
+ "index.txt: " + e.toString());
} finally {
try {
is.close();
} catch (IOException ignored) {
}
}
availableLocales = Collections.unmodifiableList(localeNames);
}
@ -103,10 +109,10 @@ public class ResourceBasedPeriodFormatterDataService extends
if (ln != null) {
String name = PATH + "pfd_" + ln + ".xml";
try {
InputStream is = ICUData.getRequiredStream(getClass(), name);
DataRecord dr = DataRecord.read(ln,
new XMLRecordReader(new InputStreamReader(
is, "UTF-8")));
InputStreamReader reader = new InputStreamReader(
ICUData.getRequiredStream(getClass(), name), "UTF-8");
DataRecord dr = DataRecord.read(ln, new XMLRecordReader(reader));
reader.close();
if (dr != null) {
// debug
// if (false && ln.equals("ar_EG")) {
@ -121,8 +127,10 @@ public class ResourceBasedPeriodFormatterDataService extends
}
} catch (UnsupportedEncodingException e) {
throw new MissingResourceException(
"Unhandled Encoding for resource " + name,
name, "");
"Unhandled encoding for resource " + name, name, "");
} catch (IOException e) {
throw new ICUUncheckedIOException(
"Failed to close() resource " + name, e);
}
} else {
throw new MissingResourceException(

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1996-2014, International Business Machines Corporation and
* Copyright (C) 1996-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -49,8 +49,9 @@ class BreakDictionary {
static void writeToFile(String inFile, String outFile)
throws FileNotFoundException, UnsupportedEncodingException, IOException {
@SuppressWarnings("resource") // Closed by getByteBufferFromInputStreamAndCloseStream().
BreakDictionary dictionary = new BreakDictionary(
ICUBinary.getByteBufferFromInputStream(new FileInputStream(inFile)));
ICUBinary.getByteBufferFromInputStreamAndCloseStream(new FileInputStream(inFile)));
PrintWriter out = null;
@ -311,4 +312,3 @@ class BreakDictionary {
return table[row * numCols + col];
}
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2009-2014, International Business Machines
* Copyright (C) 2009-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
@ -192,7 +192,7 @@ public abstract class Normalizer2 {
ByteBuffer bytes = null;
if (data != null) {
try {
bytes = ICUBinary.getByteBufferFromInputStream(data);
bytes = ICUBinary.getByteBufferFromInputStreamAndCloseStream(data);
} catch (IOException e) {
throw new ICUUncheckedIOException(e);
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2005-2014 International Business Machines Corporation and
* Copyright (C) 2005-2015 International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -66,7 +66,7 @@ public class RuleBasedBreakIterator extends BreakIterator {
*/
public static RuleBasedBreakIterator getInstanceFromCompiledRules(InputStream is) throws IOException {
RuleBasedBreakIterator This = new RuleBasedBreakIterator();
This.fRData = RBBIDataWrapper.get(ICUBinary.getByteBufferFromInputStream(is));
This.fRData = RBBIDataWrapper.get(ICUBinary.getByteBufferFromInputStreamAndCloseStream(is));
return This;
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2003-2014, International Business Machines Corporation and
* Copyright (C) 2003-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -270,7 +270,7 @@ public final class StringPrep {
*/
public StringPrep(InputStream inputStream) throws IOException{
// TODO: Add a public constructor that takes ByteBuffer directly.
this(ICUBinary.getByteBufferFromInputStream(inputStream));
this(ICUBinary.getByteBufferFromInputStreamAndCloseStream(inputStream));
}
private StringPrep(ByteBuffer bytes) throws IOException {

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2008-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2008-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.impl.javaspi;
@ -232,7 +232,11 @@ public class ICULocaleServiceProvider {
Properties spiConfigProps = new Properties();
try {
InputStream is = ClassLoader.getSystemResourceAsStream(SPI_PROP_FILE);
spiConfigProps.load(is);
try {
spiConfigProps.load(is);
} finally {
is.close();
}
String val = (String)spiConfigProps.get(SUFFIX_KEY);
if (val != null && val.length() > 0) {

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2002-2014, International Business Machines Corporation and
* Copyright (C) 2002-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -125,88 +125,75 @@ public class CollationThaiTest extends TestFmwk {
warnln("could not construct Thai collator");
return;
}
// Read in a dictionary of Thai words
BufferedReader in = null;
String fileName = "riwords.txt";
try {
in = TestUtil.getDataReader(fileName, "UTF-8");
} catch (SecurityException e) {
warnln("Security exception encountered reading test data file.");
return;
} catch (Exception e) {
try {
if (in != null) {
in.close();
}
} catch (IOException ioe) {}
errln("Error: could not open test file: " + fileName
+ ". Aborting test.");
return;
}
//
// Loop through each word in the dictionary and compare it to the previous
// word. They should be in sorted order.
//
String lastWord = "";
int line = 0;
int failed = 0;
int wordCount = 0;
BufferedReader in = null;
try {
String word = in.readLine();
while (word != null) {
line++;
// Skip comments and blank lines
if (word.length() == 0 || word.charAt(0) == 0x23) {
word = in.readLine();
continue;
}
// Show the first 8 words being compared, so we can see what's happening
++wordCount;
if (wordCount <= 8) {
logln("Word " + wordCount + ": " + word);
}
if (lastWord.length() > 0) {
// CollationTest.doTest isn't really set up to handle situations where
// the result can be equal or greater than the previous, so have to skip for now.
// Not a big deal, since we're still testing to make sure everything sorts out
// right, just not looking at the colation keys in detail...
// CollationTest.doTest(this, coll, lastWord, word, -1);
int result = coll.compare(lastWord, word);
if (result > 0) {
failed++;
if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
String msg = "--------------------------------------------\n"
+ line
+ " compare(" + lastWord
+ ", " + word + ") returned " + result
+ ", expected -1\n";
CollationKey k1, k2;
try {
String fileName = "riwords.txt";
in = TestUtil.getDataReader(fileName, "UTF-8");
//
// Loop through each word in the dictionary and compare it to the previous
// word. They should be in sorted order.
//
String lastWord = "";
String word = in.readLine();
while (word != null) {
line++;
// Skip comments and blank lines
if (word.length() == 0 || word.charAt(0) == 0x23) {
word = in.readLine();
continue;
}
// Show the first 8 words being compared, so we can see what's happening
++wordCount;
if (wordCount <= 8) {
logln("Word " + wordCount + ": " + word);
}
if (lastWord.length() > 0) {
// CollationTest.doTest isn't really set up to handle situations where
// the result can be equal or greater than the previous, so have to skip for now.
// Not a big deal, since we're still testing to make sure everything sorts out
// right, just not looking at the colation keys in detail...
// CollationTest.doTest(this, coll, lastWord, word, -1);
int result = coll.compare(lastWord, word);
if (result > 0) {
failed++;
if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
String msg = "--------------------------------------------\n" + line + " compare("
+ lastWord + ", " + word + ") returned " + result + ", expected -1\n";
CollationKey k1, k2;
k1 = coll.getCollationKey(lastWord);
k2 = coll.getCollationKey(word);
} catch (Exception e) {
errln("Fail: getCollationKey returned ");
return;
msg += "key1: " + CollationTest.prettify(k1) + "\n" + "key2: " + CollationTest.prettify(k2);
errln(msg);
}
msg += "key1: " + CollationTest.prettify(k1) + "\n"
+ "key2: " + CollationTest.prettify(k2);
errln(msg);
}
}
lastWord = word;
word = in.readLine();
}
lastWord = word;
word = in.readLine();
}
} catch (IOException e) {
errln("IOException " + e.getMessage());
} finally {
if (in == null) {
errln("Error: could not open test file. Aborting test.");
return;
} else {
try {
in.close();
} catch (IOException ignored) {
}
}
}
if (failed != 0) {
if (failed > MAX_FAILURES_TO_SHOW) {
errln("Too many failures; only the first " +

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2010-2013, International Business Machines Corporation and
* Copyright (C) 2010-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -27,57 +27,65 @@ public class BiDiConformanceTest extends TestFmwk {
public BiDiConformanceTest() {}
public void TestBidiTest() throws IOException {
BufferedReader bidiTestFile=TestUtil.getDataReader("unicode/BidiTest.txt");
Bidi ubidi=new Bidi();
ubidi.setCustomClassifier(new ConfTestBidiClassifier());
lineNumber=0;
levelsCount=0;
orderingCount=0;
errorCount=0;
BufferedReader bidiTestFile = TestUtil.getDataReader("unicode/BidiTest.txt");
try {
Bidi ubidi = new Bidi();
ubidi.setCustomClassifier(new ConfTestBidiClassifier());
lineNumber = 0;
levelsCount = 0;
orderingCount = 0;
errorCount = 0;
outerLoop:
while(errorCount<10 && (line=bidiTestFile.readLine())!=null) {
++lineNumber;
lineIndex=0;
// Remove trailing comments and whitespace.
int commentStart=line.indexOf('#');
if(commentStart>=0) {
line=line.substring(0, commentStart);
}
if(!skipWhitespace()) {
continue; // Skip empty and comment-only lines.
}
if(line.charAt(lineIndex)=='@') {
++lineIndex;
if(line.startsWith("Levels:", lineIndex)) {
lineIndex+=7;
if(!parseLevels(line.substring(lineIndex))) { break; }
} else if(line.startsWith("Reorder:", lineIndex)) {
lineIndex+=8;
if(!parseOrdering(line.substring(lineIndex))) { break; }
while (errorCount < 10 && (line = bidiTestFile.readLine()) != null) {
++lineNumber;
lineIndex = 0;
// Remove trailing comments and whitespace.
int commentStart = line.indexOf('#');
if (commentStart >= 0) {
line = line.substring(0, commentStart);
}
// Skip unknown @Xyz: ...
} else {
parseInputStringFromBiDiClasses();
if(!skipWhitespace() || line.charAt(lineIndex++)!=';') {
errln("missing ; separator on input line "+line);
return;
if (!skipWhitespace()) {
continue; // Skip empty and comment-only lines.
}
int bitset=Integer.parseInt(line.substring(lineIndex).trim(), 16);
// Loop over the bitset.
for(int i=0; i<=3; ++i) {
if((bitset&(1<<i))!=0) {
ubidi.setPara(inputString, paraLevels[i], null);
byte actualLevels[]=ubidi.getLevels();
paraLevelName=paraLevelNames[i];
if(!checkLevels(actualLevels)) {
continue outerLoop;
if (line.charAt(lineIndex) == '@') {
++lineIndex;
if (line.startsWith("Levels:", lineIndex)) {
lineIndex += 7;
if (!parseLevels(line.substring(lineIndex))) {
break;
}
if(!checkOrdering(ubidi)) {
continue outerLoop;
} else if (line.startsWith("Reorder:", lineIndex)) {
lineIndex += 8;
if (!parseOrdering(line.substring(lineIndex))) {
break;
}
}
// Skip unknown @Xyz: ...
} else {
parseInputStringFromBiDiClasses();
if (!skipWhitespace() || line.charAt(lineIndex++) != ';') {
errln("missing ; separator on input line " + line);
return;
}
int bitset = Integer.parseInt(line.substring(lineIndex).trim(), 16);
// Loop over the bitset.
for (int i = 0; i <= 3; ++i) {
if ((bitset & (1 << i)) != 0) {
ubidi.setPara(inputString, paraLevels[i], null);
byte actualLevels[] = ubidi.getLevels();
paraLevelName = paraLevelNames[i];
if (!checkLevels(actualLevels)) {
continue outerLoop;
}
if (!checkOrdering(ubidi)) {
continue outerLoop;
}
}
}
}
}
} finally {
bidiTestFile.close();
}
}
@ -154,102 +162,101 @@ outerLoop:
*******************************************************************************
*/
public void TestBidiCharacterTest() throws IOException {
BufferedReader bidiTestFile=TestUtil.getDataReader("unicode/BidiCharacterTest.txt");
Bidi ubidi=new Bidi();
lineNumber=0;
levelsCount=0;
orderingCount=0;
errorCount=0;
BufferedReader bidiTestFile = TestUtil.getDataReader("unicode/BidiCharacterTest.txt");
try {
Bidi ubidi = new Bidi();
lineNumber = 0;
levelsCount = 0;
orderingCount = 0;
errorCount = 0;
outerLoop:
while(errorCount<20 && (line=bidiTestFile.readLine())!=null) {
++lineNumber;
paraLevelName="N/A";
inputString="N/A";
lineIndex=0;
// Remove trailing comments and whitespace.
int commentStart=line.indexOf('#');
if(commentStart>=0) {
line=line.substring(0, commentStart);
}
if(!skipWhitespace()) {
continue; // Skip empty and comment-only lines.
}
String[] parts=line.split(";");
if(parts.length<4) {
errorCount++;
errln(" on line " + lineNumber + ": Missing ; separator on line: " + line);
continue;
}
// Parse the code point string in field 0.
try {
inputStringBuilder.delete(0, inputStringBuilder.length());
for(String cp : parts[0].trim().split("[ \t]+")) {
inputStringBuilder.appendCodePoint(Integer.parseInt(cp, 16));
while (errorCount < 20 && (line = bidiTestFile.readLine()) != null) {
++lineNumber;
paraLevelName = "N/A";
inputString = "N/A";
lineIndex = 0;
// Remove trailing comments and whitespace.
int commentStart = line.indexOf('#');
if (commentStart >= 0) {
line = line.substring(0, commentStart);
}
inputString=inputStringBuilder.toString();
} catch(Exception e) {
errln(" ------------ Invalid string in field 0 on line '"+line+"'");
++errorCount;
continue;
}
int paraDirection=intFromString(parts[1].trim());
byte paraLevel;
if(paraDirection==0) {
paraLevel=0;
paraLevelName="LTR";
}
else if(paraDirection==1) {
paraLevel=1;
paraLevelName="RTL";
}
else if(paraDirection==2) {
paraLevel=Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
paraLevelName="Auto/LTR";
}
else if(paraDirection==3) {
paraLevel=Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT;
paraLevelName="Auto/RTL";
}
else if(paraDirection<0 && -paraDirection<=(Bidi.MAX_EXPLICIT_LEVEL+1)) {
paraLevel=(byte)(-paraDirection);
paraLevelName=Byte.toString(paraLevel);
}
else {
errorCount++;
errln(" on line " + lineNumber + ": Input paragraph direction incorrect at " + line);
continue;
}
int resolvedParaLevel=intFromString(parts[2].trim());
if(resolvedParaLevel<0 || resolvedParaLevel>(Bidi.MAX_EXPLICIT_LEVEL+1)) {
errorCount++;
errln(" on line " + lineNumber + ": Resolved paragraph level incorrect at " + line);
continue;
}
if(!parseLevels(parts[3])) {
continue;
}
if(parts.length>4) {
if(!parseOrdering(parts[4]))
if (!skipWhitespace()) {
continue; // Skip empty and comment-only lines.
}
String[] parts = line.split(";");
if (parts.length < 4) {
errorCount++;
errln(" on line " + lineNumber + ": Missing ; separator on line: " + line);
continue;
}
else
orderingCount=-1;
}
// Parse the code point string in field 0.
try {
inputStringBuilder.delete(0, inputStringBuilder.length());
for (String cp : parts[0].trim().split("[ \t]+")) {
inputStringBuilder.appendCodePoint(Integer.parseInt(cp, 16));
}
inputString = inputStringBuilder.toString();
} catch (Exception e) {
errln(" ------------ Invalid string in field 0 on line '" + line + "'");
++errorCount;
continue;
}
int paraDirection = intFromString(parts[1].trim());
byte paraLevel;
if (paraDirection == 0) {
paraLevel = 0;
paraLevelName = "LTR";
} else if (paraDirection == 1) {
paraLevel = 1;
paraLevelName = "RTL";
} else if (paraDirection == 2) {
paraLevel = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
paraLevelName = "Auto/LTR";
} else if (paraDirection == 3) {
paraLevel = Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT;
paraLevelName = "Auto/RTL";
} else if (paraDirection < 0 && -paraDirection <= (Bidi.MAX_EXPLICIT_LEVEL + 1)) {
paraLevel = (byte) (-paraDirection);
paraLevelName = Byte.toString(paraLevel);
} else {
errorCount++;
errln(" on line " + lineNumber + ": Input paragraph direction incorrect at " + line);
continue;
}
int resolvedParaLevel = intFromString(parts[2].trim());
if (resolvedParaLevel < 0 || resolvedParaLevel > (Bidi.MAX_EXPLICIT_LEVEL + 1)) {
errorCount++;
errln(" on line " + lineNumber + ": Resolved paragraph level incorrect at " + line);
continue;
}
if (!parseLevels(parts[3])) {
continue;
}
if (parts.length > 4) {
if (!parseOrdering(parts[4])) {
continue;
}
} else {
orderingCount = -1;
}
ubidi.setPara(inputString, paraLevel, null);
byte actualParaLevel=ubidi.getParaLevel();
if(actualParaLevel!=resolvedParaLevel) {
errln(" ------------ Wrong resolved paragraph level; expected "
+resolvedParaLevel+" actual "
+actualParaLevel);
printErrorLine();
}
byte[] actualLevels=ubidi.getLevels();
if(!checkLevels(actualLevels)) {
continue outerLoop;
}
if(!checkOrdering(ubidi)) {
continue outerLoop;
ubidi.setPara(inputString, paraLevel, null);
byte actualParaLevel = ubidi.getParaLevel();
if (actualParaLevel != resolvedParaLevel) {
errln(" ------------ Wrong resolved paragraph level; expected " + resolvedParaLevel + " actual "
+ actualParaLevel);
printErrorLine();
}
byte[] actualLevels = ubidi.getLevels();
if (!checkLevels(actualLevels)) {
continue outerLoop;
}
if (!checkOrdering(ubidi)) {
continue outerLoop;
}
}
} finally {
bidiTestFile.close();
}
}

View file

@ -177,7 +177,11 @@ public class TestCharsetDetector extends TestFmwk
CheckAssert(s.equals(retrievedS));
reader = det.getReader(new ByteArrayInputStream(bytes), "UTF-8");
CheckAssert(s.equals(stringFromReader(reader)));
try {
CheckAssert(s.equals(stringFromReader(reader)));
} finally {
reader.close();
}
det.setDeclaredEncoding("UTF-8"); // Jitterbug 4451, for coverage
}
@ -340,7 +344,12 @@ public class TestCharsetDetector extends TestFmwk
DocumentBuilder builder = factory.newDocumentBuilder();
// Parse the xml content from the test case file.
Document doc = builder.parse(is, null);
Document doc;
try {
doc = builder.parse(is, null);
} finally {
is.close();
}
Element root = doc.getDocumentElement();
NodeList testCases = root.getElementsByTagName("test-case");

View file

@ -1,9 +1,9 @@
/*
******************************************************************************
* Copyright (C) 2007-2010, International Business Machines Corporation and *
* others. All Rights Reserved. *
******************************************************************************
*/
******************************************************************************
* Copyright (C) 2007-2015, International Business Machines Corporation and
* others. All Rights Reserved.
******************************************************************************
*/
// Copyright 2006 Google Inc. All Rights Reserved.
@ -84,7 +84,11 @@ public class LanguageTestRoot extends TestFmwk implements TimeUnitConstants {
+ "' is null");
}
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
data = new FileTestData(isr);
try {
data = new FileTestData(isr);
} finally {
isr.close();
}
} catch (Exception e) {
System.err.println(e.getMessage());
// swallow any exception
@ -802,4 +806,3 @@ class Element {
return null;
}
}

View file

@ -1,8 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2001-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2001-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -13,6 +12,7 @@
package com.ibm.icu.dev.test.format;
import java.io.IOException;
import java.math.BigInteger;
import java.text.AttributedCharacterIterator;
import java.text.FieldPosition;
@ -1709,6 +1709,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
/*9*/ "strict=", // true or false
};
@SuppressWarnings("resource") // InputStream is will be closed by the ResourceReader.
public void TestCases() {
String caseFileName = "NumberFormatTestCases.txt";
java.io.InputStream is = NumberFormatTest.class.getResourceAsStream(caseFileName);
@ -1886,6 +1887,11 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
}
} catch (java.io.IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException ignored) {
}
}
}

View file

@ -1,13 +1,14 @@
/**
*******************************************************************************
* Copyright (C) 1996-2014, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
*******************************************************************************
* Copyright (C) 1996-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.dev.test.lang;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
@ -691,9 +692,9 @@ public final class UCharacterTest extends TestFmwk
Normalizer2 nfc = Normalizer2.getNFCInstance();
Normalizer2 nfkc = Normalizer2.getNFKCInstance();
try
{
BufferedReader input = TestUtil.getDataReader("unicode/UnicodeData.txt");
BufferedReader input = null;
try {
input = TestUtil.getDataReader("unicode/UnicodeData.txt");
int numErrors = 0;
for (;;) {
@ -906,17 +907,20 @@ public final class UCharacterTest extends TestFmwk
break;
}
}
input.close();
if(numErrors > 0){
warnln("Could not find unames.icu");
}
}
catch (Exception e)
{
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ignored) {
}
}
}
if (UCharacter.UnicodeBlock.of(0x0041)
!= UCharacter.UnicodeBlock.BASIC_LATIN
|| UCharacter.getIntPropertyValue(0x41, UProperty.BLOCK)

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 1996-2010, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 1996-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -53,7 +53,6 @@ public class ConformanceTest extends TestFmwk {
}
public void runConformance(String fileName, int options) throws Exception{
BufferedReader input = null;
String line = null;
String[] fields = new String[5];
StringBuffer buf = new StringBuffer();
@ -61,6 +60,7 @@ public class ConformanceTest extends TestFmwk {
int failCount = 0;
UnicodeSet other = new UnicodeSet(0, 0x10ffff);
int c=0;
BufferedReader input = null;
try {
input = TestUtil.getDataReader(fileName);
for (int count = 0;;++count) {
@ -108,18 +108,18 @@ public class ConformanceTest extends TestFmwk {
}
}
} catch (IOException ex) {
if (input != null) {
try {
input.close();
} catch (Exception ex2) {
System.out.print("");
}
}
ex.printStackTrace();
throw new IllegalArgumentException("Couldn't read file "
+ ex.getClass().getName() + " " + ex.getMessage()
+ " line = " + line
);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ignored) {
}
}
}
if (failCount != 0) {

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 1996-2008, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 1996-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -48,7 +48,6 @@ public class UnicodeNormalizerConformanceTest extends TestFmwk {
* This file must be located at the path specified as TEST_SUITE_FILE.
*/
public void TestConformance() throws Exception{
BufferedReader input = null;
String line = null;
String[] fields = new String[5];
StringBuffer buf = new StringBuffer();
@ -56,6 +55,7 @@ public class UnicodeNormalizerConformanceTest extends TestFmwk {
int failCount = 0;
UnicodeSet other = new UnicodeSet(0, 0x10ffff);
int c=0;
BufferedReader input = null;
try {
input = TestUtil.getDataReader("unicode/NormalizationTest.txt");
for (int count = 0;;++count) {
@ -103,18 +103,18 @@ public class UnicodeNormalizerConformanceTest extends TestFmwk {
}
}
} catch (IOException ex) {
if (input != null) {
try {
input.close();
} catch (Exception ex2) {
System.out.print("");
}
}
ex.printStackTrace();
throw new IllegalArgumentException("Couldn't read file "
+ ex.getClass().getName() + " " + ex.getMessage()
+ " line = " + line
);
} finally {
if (input != null) {
try {
input.close();
} catch (Exception ignored) {
}
}
}
if (failCount != 0) {

View file

@ -1,7 +1,7 @@
/*
* Created on May 5, 2004
*
* Copyright (C) 2004-2013 International Business Machines Corporation and others.
* Copyright (C) 2004-2015 International Business Machines Corporation and others.
* All Rights Reserved.
*
*/
@ -56,33 +56,40 @@ public void TestExtended() {
//
// Open and read the test data file.
//
InputStreamReader isr = null;
StringBuffer testFileBuf = new StringBuffer();
StringBuffer testFileBuf = new StringBuffer();
InputStream is = null;
try {
InputStream is = RBBITestExtended.class.getResourceAsStream("rbbitst.txt");
is = RBBITestExtended.class.getResourceAsStream("rbbitst.txt");
if (is == null) {
errln("Could not open test data file rbbitst.txt");
return;
}
isr = new InputStreamReader(is, "UTF-8");
int c;
int count = 0;
for (;;) {
c = isr.read();
if (c < 0) {
break;
}
count++;
if (c==0xFEFF && count==1) {
// BOM in the test data file. Discard it.
continue;
}
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
try {
int c;
int count = 0;
for (;;) {
c = isr.read();
if (c < 0) {
break;
}
count++;
if (c == 0xFEFF && count == 1) {
// BOM in the test data file. Discard it.
continue;
}
UTF16.append(testFileBuf, c);
UTF16.append(testFileBuf, c);
}
} finally {
isr.close();
}
} catch (IOException e) {
errln(e.toString());
try {
is.close();
} catch (IOException ignored) {
}
return;
}

View file

@ -1,16 +1,19 @@
/*
*******************************************************************************
* Copyright (C) 1996-2013, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 1996-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
*/
package com.ibm.icu.dev.test.serializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.JarURLConnection;
@ -83,40 +86,45 @@ public class CompatibilityTest extends TestFmwk
protected void execute() throws Exception
{
if (params.inDocMode()) {
// nothing to execute
} else if (!params.stack.included) {
++params.invalidCount;
} else {
params.testCount += 1;
try {
if (params.inDocMode()) {
// nothing to execute
} else if (!params.stack.included) {
++params.invalidCount;
} else {
params.testCount += 1;
try {
ObjectInputStream in = new ObjectInputStream(inputStream);
Object inputObjects[] = (Object[]) in.readObject();
Object testObjects[] = handler.getTestObjects();
in.close();
inputStream.close();
// TODO: add equality test...
// The commented out code below does that,
// but some test objects don't define an equals() method,
// and the default method is the same as the "==" operator...
for (int i = 0; i < testObjects.length; i += 1) {
// if (! inputObjects[i].equals(testObjects[i])) {
// errln("Input object " + i + " failed equality test.");
// }
if (! handler.hasSameBehavior(inputObjects[i], testObjects[i])) {
warnln("Input object " + i + " failed behavior test.");
try {
ObjectInputStream in = new ObjectInputStream(inputStream);
Object inputObjects[] = (Object[]) in.readObject();
Object testObjects[] = handler.getTestObjects();
in.close();
inputStream.close();
// TODO: add equality test...
// The commented out code below does that,
// but some test objects don't define an equals() method,
// and the default method is the same as the "==" operator...
for (int i = 0; i < testObjects.length; i += 1) {
// if (! inputObjects[i].equals(testObjects[i])) {
// errln("Input object " + i + " failed equality test.");
// }
if (!handler.hasSameBehavior(inputObjects[i], testObjects[i])) {
warnln("Input object " + i + " failed behavior test.");
}
}
} catch (MissingResourceException e) {
warnln("Could not load the data. " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
errln("Exception: " + e.toString());
}
}catch (MissingResourceException e){
warnln("Could not load the data. "+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
errln("Exception: " + e.toString());
}
} finally {
inputStream.close();
inputStream = null;
}
}
}
@ -178,10 +186,9 @@ public class CompatibilityTest extends TestFmwk
}
}
InputStream is;
try {
is = new FileInputStream(file);
@SuppressWarnings("resource") // Closed by HandlerTarget.execute().
InputStream is = new FileInputStream(file);
target.add(className, is);
} catch (FileNotFoundException e) {
errln("Exception: " + e.toString());
@ -194,13 +201,29 @@ public class CompatibilityTest extends TestFmwk
return target;
}
private static InputStream copyInputStream(InputStream in) throws IOException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while (true) {
int r = in.read(buf);
if (r == -1) {
break;
}
out.write(buf, 0, r);
}
return new ByteArrayInputStream(out.toByteArray());
} finally {
in.close();
}
}
private Target getJarTargets(URL jarURL)
{
String prefix = jarURL.getPath();
String currentDir = null;
int ix = prefix.indexOf("!/");
JarFile jarFile;
FolderTarget target = null;
if (ix >= 0) {
@ -209,55 +232,59 @@ public class CompatibilityTest extends TestFmwk
try {
JarURLConnection conn = (JarURLConnection) jarURL.openConnection();
JarFile jarFile = conn.getJarFile();
try {
Enumeration entries = jarFile.entries();
element_loop:
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
String name = entry.getName();
jarFile = conn.getJarFile();
if (name.startsWith(prefix)) {
name = name.substring(prefix.length());
Enumeration entries = jarFile.entries();
if (!entry.isDirectory()) {
int dx = name.lastIndexOf("/");
String dirName = name.substring(1, dx);
String filename = name.substring(dx + 1);
element_loop:
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry)entries.nextElement();
String name = entry.getName();
if (name.startsWith(prefix)) {
name = name.substring(prefix.length());
if (!dirName.equals(currentDir)) {
currentDir = dirName;
if (! entry.isDirectory()) {
int dx = name.lastIndexOf("/");
String dirName = name.substring(1, dx);
String filename = name.substring(dx + 1);
FolderTarget newTarget = new FolderTarget(currentDir);
if (! dirName.equals(currentDir)) {
currentDir = dirName;
FolderTarget newTarget = new FolderTarget(currentDir);
newTarget.setNext(target);
target = newTarget;
}
int xx = filename.indexOf(".dat");
if (xx > 0) {
String className = filename.substring(0, xx);
// Skip some cases which do not work well
for (int i = 0; i < SKIP_CASES.length; i++) {
if (dirName.equals(SKIP_CASES[i][0]) && filename.equals(SKIP_CASES[i][1])) {
logln("Skipping test case - " + dirName + "/" + className);
continue element_loop;
}
newTarget.setNext(target);
target = newTarget;
}
target.add(className, jarFile.getInputStream(entry));
int xx = filename.indexOf(".dat");
if (xx > 0) {
String className = filename.substring(0, xx);
// Skip some cases which do not work well
for (int i = 0; i < SKIP_CASES.length; i++) {
if (dirName.equals(SKIP_CASES[i][0]) && filename.equals(SKIP_CASES[i][1])) {
logln("Skipping test case - " + dirName + "/" + className);
continue element_loop;
}
}
// The InputStream object returned by JarFile.getInputStream() will
// no longer be useable after JarFile.close() has been called. It's
// therefore necessary to make a copy of it here.
target.add(className, copyInputStream(jarFile.getInputStream(entry)));
}
}
}
}
} finally {
jarFile.close();
}
} catch (Exception e) {
errln("jar error: " + e.getMessage());
}
return target;
}

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2005-2013, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2005-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
*/
@ -66,18 +66,22 @@ public class CoverageTest extends CompatibilityTest implements URLHandler.URLVis
private void writeFile(String className, byte bytes[])
{
File file = new File(path + File.separator + className + ".dat");
FileOutputStream stream;
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file);
stream.write(bytes);
stream.close();
} catch (Exception e) {
System.out.print(" - can't write file!");
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException ignored) {
}
}
}
}
private void add(String className, int classModifiers, byte bytes[])
{
CoverageTarget newTarget = new CoverageTarget(className, classModifiers, bytes);

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2005-2007, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2005-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*******************************************************************************
@ -10,7 +10,6 @@
package com.ibm.icu.dev.test.stringprep;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
@ -246,9 +245,8 @@ public class IDNAConformanceTest extends TestFmwk {
UnsupportedEncodingException {
TreeMap result = new TreeMap();
BufferedReader in = TestUtil.getDataReader("IDNATestInput.txt", "utf-8");
try {
BufferedReader in = TestUtil.getDataReader("IDNATestInput.txt", "utf-8");
String tempStr = null;
int records = 0;
boolean firstLine = true;
@ -270,7 +268,7 @@ public class IDNAConformanceTest extends TestFmwk {
String attr = "";//attribute
String body = "";//value
//get attr and body from line input, and then set them into each hash item.
//get attr and body from line input, and then set them into each hash item.
int postion = tempStr.indexOf(":");
if (postion > -1) {
attr = tempStr.substring(0, postion).trim();
@ -298,14 +296,10 @@ public class IDNAConformanceTest extends TestFmwk {
continue;
}
}
} catch (UnsupportedEncodingException e) {
throw e;
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
in.close();
}
return result;
}
}
}
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2009-2014, International Business Machines Corporation and
* Copyright (C) 2009-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -90,13 +90,22 @@ public class SpoofCheckerTest extends TestFmwk {
Reader confusablesWholeScript;
try {
SpoofChecker rsc = null;
fileName = "unicode/confusables.txt";
confusables = TestUtil.getDataReader(fileName, "UTF-8");
fileName = "unicode/confusablesWholeScript.txt";
confusablesWholeScript = TestUtil.getDataReader(fileName, "UTF-8");
try {
fileName = "unicode/confusablesWholeScript.txt";
confusablesWholeScript = TestUtil.getDataReader(fileName, "UTF-8");
try {
rsc = new SpoofChecker.Builder().setData(confusables, confusablesWholeScript).build();
} finally {
confusablesWholeScript.close();
}
} finally {
confusables.close();
}
SpoofChecker rsc = new SpoofChecker.Builder().setData(confusables, confusablesWholeScript)
.build();
if (rsc == null) {
errln("FAIL: null SpoofChecker");
return;

View file

@ -1,13 +1,12 @@
/**
*******************************************************************************
* Copyright (C) 2001-2014, International Business Machines Corporation and
* Copyright (C) 2001-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.dev.test.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.JarURLConnection;
import java.net.URL;
@ -59,14 +58,17 @@ public final class ICUResourceBundleTest extends TestFmwk {
JarEntry je = jc.getJarEntry();
logln("jar entry: " + je.toString());
} else {
InputStream is = c.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(c.getInputStream()));
logln("input stream:");
InputStreamReader r = new InputStreamReader(is);
BufferedReader br = new BufferedReader(r);
String line = null;
int n = 0;
while ((line = br.readLine()) != null) {
logln(" " + ++n + ": " + line);
try {
String line = null;
int n = 0;
while ((line = br.readLine()) != null) {
logln(" " + ++n + ": " + line);
}
} finally {
br.close();
}
}
}

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2009-2014, International Business Machines Corporation and
* Copyright (C) 2009-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -715,14 +715,23 @@ public class Trie2Test extends TestFmwk {
String fileName32 = "Trie2Test." + serializedName + ".32.tri2";
InputStream is = Trie2Test.class.getResourceAsStream(fileName16);
Trie2 trie16 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStream(is));
Trie2 trie16;
try {
trie16 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStreamAndCloseStream(is));
} finally {
is.close();
}
trieGettersTest(testName, trie16, checkRanges);
is = Trie2Test.class.getResourceAsStream(fileName32);
Trie2 trie32 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStream(is));
Trie2 trie32;
try {
trie32 = Trie2.createFromSerialized(ICUBinary.getByteBufferFromInputStreamAndCloseStream(is));
} finally {
is.close();
}
trieGettersTest(testName, trie32, checkRanges);
// Run the same tests against locally contructed Tries.
Trie2Writable trieW = genTrieFromSetRanges(setRanges);
trieGettersTest(testName, trieW, checkRanges);

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2009, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2009-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.dev.test.translit;
@ -170,7 +170,12 @@ public class RegexUtilitiesTest extends TestFmwk {
String result;
if (test.endsWith(".txt")) {
java.io.InputStream is = RegexUtilitiesTest.class.getResourceAsStream(test);
List lines = UnicodeRegex.appendLines(new ArrayList(), is, "UTF-8");
List lines;
try {
lines = UnicodeRegex.appendLines(new ArrayList(), is, "UTF-8");
} finally {
is.close();
}
result = regex.compileBnf(lines);
} else {
result = regex.compileBnf(test);

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
* Copyright (C) 2002-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
* Copyright (C) 2002-2015, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
package com.ibm.icu.dev.util;
@ -23,7 +23,11 @@ public class FileUtilities {
InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr, 32*1024);
*/
appendBufferedReader(br, output, replacementList);
try {
appendBufferedReader(br, output, replacementList);
} finally {
br.close();
}
}
public static void appendBufferedReader(BufferedReader br,