本文整理汇总了Java中com.puppycrawl.tools.checkstyle.utils.CommonUtils.close方法的典型用法代码示例。如果您正苦于以下问题:Java CommonUtils.close方法的具体用法?Java CommonUtils.close怎么用?Java CommonUtils.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.puppycrawl.tools.checkstyle.utils.CommonUtils
的用法示例。
在下文中一共展示了CommonUtils.close方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFile
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; //导入方法依赖的package包/类
/**
* Reads file using specific decoder and returns all its content as a String.
* @param inputFile File to read
* @param decoder Charset decoder
* @return File's text
* @throws IOException Unable to open or read the file
*/
private static String readFile(final File inputFile, final CharsetDecoder decoder)
throws IOException {
if (!inputFile.exists()) {
throw new FileNotFoundException(inputFile.getPath() + " (No such file or directory)");
}
final StringBuilder buf = new StringBuilder(1024);
final FileInputStream stream = new FileInputStream(inputFile);
final Reader reader = new InputStreamReader(stream, decoder);
try {
final char[] chars = new char[READ_BUFFER_SIZE];
while (true) {
final int len = reader.read(chars);
if (len == -1) {
break;
}
buf.append(chars, 0, len);
}
}
finally {
CommonUtils.close(reader);
}
return buf.toString();
}
示例2: FileText
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; //导入方法依赖的package包/类
/**
* Creates a new file text representation.
*
* <p>The file will be read using the specified encoding, replacing
* malformed input and unmappable characters with the default
* replacement character.
*
* @param file the name of the file
* @param charsetName the encoding to use when reading the file
* @throws NullPointerException if the text is null
* @throws IOException if the file could not be read
*/
public FileText(File file, String charsetName) throws IOException {
this.file = file;
// We use our own decoder, to be sure we have complete control
// about replacements.
final CharsetDecoder decoder;
try {
charset = Charset.forName(charsetName);
decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
catch (final UnsupportedCharsetException ex) {
final String message = "Unsupported charset: " + charsetName;
throw new IllegalStateException(message, ex);
}
fullText = readFile(file, decoder);
// Use the BufferedReader to break down the lines as this
// is about 30% faster than using the
// LINE_TERMINATOR.split(fullText, -1) method
final BufferedReader reader = new BufferedReader(new StringReader(fullText));
try {
final ArrayList<String> textLines = new ArrayList<String>();
while (true) {
final String line = reader.readLine();
if (line == null) {
break;
}
textLines.add(line);
}
lines = textLines.toArray(new String[textLines.size()]);
}
finally {
CommonUtils.close(reader);
}
}
示例3: testSupportedCharset
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; //导入方法依赖的package包/类
@Test
public void testSupportedCharset() throws IOException {
//check if reader finally closed
mockStatic(CommonUtils.class);
doNothing().when(CommonUtils.class);
CommonUtils.close(any(Reader.class));
final String charsetName = StandardCharsets.ISO_8859_1.name();
final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
charsetName);
assertEquals("Invalid charset name", charsetName, fileText.getCharset().name());
verifyStatic(times(2));
CommonUtils.close(any(Reader.class));
}
示例4: testCreateListenerWithLocationIllegalStateException
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; //导入方法依赖的package包/类
@Test
public void testCreateListenerWithLocationIllegalStateException() throws Exception {
mockStatic(CommonUtils.class);
doNothing().when(CommonUtils.class);
CommonUtils.close(any(OutputStream.class));
final Method method = Main.class.getDeclaredMethod("createListener", String.class,
String.class);
method.setAccessible(true);
final String outDir = "myfolder123";
try {
method.invoke(null, "myformat", outDir);
fail("InvocationTargetException is expected");
}
catch (InvocationTargetException ex) {
final LocalizedMessage createListenerMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, Main.CREATE_LISTENER_EXCEPTION,
new String[] {"myformat", "plain", "xml"}, null, getClass(), null);
assertEquals("Invalid error message",
createListenerMessage.getMessage(), ex.getCause().getLocalizedMessage());
assertTrue("Invalid error cause",
ex.getCause() instanceof IllegalStateException);
}
finally {
verifyStatic(times(1));
final ArgumentCaptor<OutputStream> out =
ArgumentCaptor.forClass(OutputStream.class);
CommonUtils.close(out.capture());
out.getValue().close();
// method creates output folder
FileUtils.deleteQuietly(new File(outDir));
}
}
示例5: createListener
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; //导入方法依赖的package包/类
/**
* Creates the audit listener.
*
* @param format format of the audit listener
* @param outputLocation the location of output
* @return a fresh new {@code AuditListener}
* @exception FileNotFoundException when provided output location is not found
* @noinspection IOResourceOpenedButNotSafelyClosed
*/
private static AuditListener createListener(String format,
String outputLocation)
throws FileNotFoundException {
// setup the output stream
final OutputStream out;
final AutomaticBean.OutputStreamOptions closeOutputStream;
if (outputLocation == null) {
out = System.out;
closeOutputStream = AutomaticBean.OutputStreamOptions.NONE;
}
else {
out = new FileOutputStream(outputLocation);
closeOutputStream = AutomaticBean.OutputStreamOptions.CLOSE;
}
// setup a listener
final AuditListener listener;
if (XML_FORMAT_NAME.equals(format)) {
listener = new XMLLogger(out, closeOutputStream);
}
else if (PLAIN_FORMAT_NAME.equals(format)) {
listener = new DefaultLogger(out, closeOutputStream, out,
AutomaticBean.OutputStreamOptions.NONE);
}
else {
if (closeOutputStream == AutomaticBean.OutputStreamOptions.CLOSE) {
CommonUtils.close(out);
}
final LocalizedMessage outputFormatExceptionMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, CREATE_LISTENER_EXCEPTION,
new String[] {format, PLAIN_FORMAT_NAME, XML_FORMAT_NAME}, null,
Main.class, null);
throw new IllegalStateException(outputFormatExceptionMessage.getMessage());
}
return listener;
}