本文整理汇总了Java中libcore.io.IoUtils类的典型用法代码示例。如果您正苦于以下问题:Java IoUtils类的具体用法?Java IoUtils怎么用?Java IoUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IoUtils类属于libcore.io包,在下文中一共展示了IoUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEligiblePackages
import libcore.io.IoUtils; //导入依赖的package包/类
private List<String> getEligiblePackages(Uri contentUri, Activity context) throws IOException {
List<String> results = new LinkedList<>();
ParcelFileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(contentUri, "r");
FileInputStream fileInputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
ZipInputStream inputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry;
while ((zipEntry = inputStream.getNextEntry()) != null) {
String zipEntryPath = zipEntry.getName();
if (zipEntryPath.startsWith(ContentProviderBackupConfigurationBuilder.DEFAULT_FULL_BACKUP_DIRECTORY)) {
String fileName = new File(zipEntryPath).getName();
results.add(fileName);
}
inputStream.closeEntry();
}
IoUtils.closeQuietly(inputStream);
IoUtils.closeQuietly(fileDescriptor.getFileDescriptor());
return results;
}
示例2: test_SSL_do_handshake_client_timeout
import libcore.io.IoUtils; //导入依赖的package包/类
@Test
public void test_SSL_do_handshake_client_timeout() throws Exception {
// client timeout
final ServerSocket listener = newServerSocket();
Socket serverSocket = null;
try {
Hooks cHooks = new Hooks();
Hooks sHooks = new ServerHooks(getServerPrivateKey(), getEncodedServerCertificates());
Future<TestSSLHandshakeCallbacks> client =
handshake(listener, 1, true, cHooks, null, null);
Future<TestSSLHandshakeCallbacks> server =
handshake(listener, -1, false, sHooks, null, null);
serverSocket = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getSocket();
client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
fail();
} catch (ExecutionException expected) {
assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
} finally {
// Manually close peer socket when testing timeout
IoUtils.closeQuietly(serverSocket);
}
}
示例3: test_SSL_do_handshake_server_timeout
import libcore.io.IoUtils; //导入依赖的package包/类
@Test
public void test_SSL_do_handshake_server_timeout() throws Exception {
// server timeout
final ServerSocket listener = newServerSocket();
Socket clientSocket = null;
try {
Hooks cHooks = new Hooks();
Hooks sHooks = new ServerHooks(getServerPrivateKey(), getEncodedServerCertificates());
Future<TestSSLHandshakeCallbacks> client =
handshake(listener, -1, true, cHooks, null, null);
Future<TestSSLHandshakeCallbacks> server =
handshake(listener, 1, false, sHooks, null, null);
clientSocket = client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getSocket();
server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
fail();
} catch (ExecutionException expected) {
assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
} finally {
// Manually close peer socket when testing timeout
IoUtils.closeQuietly(clientSocket);
}
}
示例4: format
import libcore.io.IoUtils; //导入依赖的package包/类
/**
* Converts a {@link LogRecord} object into a human readable string
* representation.
*
* @param r
* the log record to be formatted into a string.
* @return the formatted string.
*/
@Override
public String format(LogRecord r) {
StringBuilder sb = new StringBuilder();
sb.append(MessageFormat.format("{0, date} {0, time} ",
new Object[] { new Date(r.getMillis()) }));
sb.append(r.getSourceClassName()).append(" ");
sb.append(r.getSourceMethodName()).append(System.lineSeparator());
sb.append(r.getLevel().getName()).append(": ");
sb.append(formatMessage(r)).append(System.lineSeparator());
if (r.getThrown() != null) {
sb.append("Throwable occurred: ");
Throwable t = r.getThrown();
PrintWriter pw = null;
try {
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw);
t.printStackTrace(pw);
sb.append(sw.toString());
} finally {
IoUtils.closeQuietly(pw);
}
}
return sb.toString();
}
示例5: Scanner
import libcore.io.IoUtils; //导入依赖的package包/类
/**
* Creates a {@code Scanner} with the specified {@code File} as input. The specified charset
* is applied when reading the file.
*
* @param src
* the file to be scanned.
* @param charsetName
* the name of the encoding type of the file.
* @throws FileNotFoundException
* if the specified file does not exist.
* @throws IllegalArgumentException
* if the specified coding does not exist.
*/
public Scanner(File src, String charsetName) throws FileNotFoundException {
if (src == null) {
throw new NullPointerException("src == null");
}
FileInputStream fis = new FileInputStream(src);
if (charsetName == null) {
throw new IllegalArgumentException("charsetName == null");
}
InputStreamReader streamReader;
try {
streamReader = new InputStreamReader(fis, charsetName);
} catch (UnsupportedEncodingException e) {
IoUtils.closeQuietly(fis);
throw new IllegalArgumentException(e.getMessage());
}
initialize(streamReader);
}
示例6: SelectorImpl
import libcore.io.IoUtils; //导入依赖的package包/类
public SelectorImpl(SelectorProvider selectorProvider) throws IOException {
super(selectorProvider);
/*
* Create a pipe to trigger wakeup. We can't use a NIO pipe because it
* would be closed if the selecting thread is interrupted. Also
* configure the pipe so we can fully drain it without blocking.
*/
try {
FileDescriptor[] pipeFds = Libcore.os.pipe();
wakeupIn = pipeFds[0];
wakeupOut = pipeFds[1];
IoUtils.setBlocking(wakeupIn, false);
pollFds.add(new StructPollfd());
setPollFd(0, wakeupIn, POLLIN, null);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
}
}
示例7: createNewFile
import libcore.io.IoUtils; //导入依赖的package包/类
/**
* Creates a new, empty file on the file system according to the path
* information stored in this file. This method returns true if it creates
* a file, false if the file already existed. Note that it returns false
* even if the file is not a file (because it's a directory, say).
*
* <p>This method is not generally useful. For creating temporary files,
* use {@link #createTempFile} instead. For reading/writing files, use {@link FileInputStream},
* {@link FileOutputStream}, or {@link RandomAccessFile}, all of which can create files.
*
* <p>Note that this method does <i>not</i> throw {@code IOException} if the file
* already exists, even if it's not a regular file. Callers should always check the
* return value, and may additionally want to call {@link #isFile}.
*
* @return true if the file has been created, false if it
* already exists.
* @throws IOException if it's not possible to create the file.
*/
public boolean createNewFile() throws IOException {
if (0 == path.length()) {
throw new IOException("No such file or directory");
}
if (isDirectory()) { // true for paths like "dir/..", which can't be files.
throw new IOException("Cannot create: " + path);
}
FileDescriptor fd = null;
try {
// On Android, we don't want default permissions to allow global access.
fd = Libcore.os.open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
return true;
} catch (ErrnoException errnoException) {
if (errnoException.errno == EEXIST) {
// The file already exists.
return false;
}
throw errnoException.rethrowAsIOException();
} finally {
IoUtils.close(fd); // TODO: should we suppress IOExceptions thrown here?
}
}
示例8: run
import libcore.io.IoUtils; //导入依赖的package包/类
@Override
public void run() {
try {
FileOutputStream fos = new FileOutputStream(mOutFd);
try {
byte[] buffer = new byte[TOTAL_SIZE];
for (int i = 0; i < buffer.length; ++i) {
buffer[i] = (byte) i;
}
fos.write(buffer);
} finally {
IoUtils.closeQuietly(fos);
IoUtils.close(mOutFd);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例9: close
import libcore.io.IoUtils; //导入依赖的package包/类
@Override
public void close() throws IOException {
try {
super.close();
} finally {
synchronized (this) {
if (fd != null && fd.valid()) {
try {
IoUtils.close(fd);
} finally {
fd = null;
}
}
}
}
}
示例10: close
import libcore.io.IoUtils; //导入依赖的package包/类
/**
* Closes this stream. This implementation closes the underlying operating
* system resources allocated to represent this stream.
*
* @throws IOException
* if an error occurs attempting to close this stream.
*/
@Override
public void close() throws IOException {
if (fd == null) {
// if fd is null, then the underlying file is not opened, so nothing
// to close
return;
}
if (channel != null) {
synchronized (channel) {
if (channel.isOpen() && fd.descriptor >= 0) {
channel.close();
}
}
}
synchronized (this) {
if (fd.valid() && innerFD) {
IoUtils.close(fd);
}
}
}
示例11: install
import libcore.io.IoUtils; //导入依赖的package包/类
/***
* Creates a new HTTP response cache and {@link ResponseCache#setDefault
* sets it} as the system default cache.
*
* @param directory the directory to hold cache data.
* @param maxSize the maximum size of the cache in bytes.
* @return the newly-installed cache
* @throws IOException if {@code directory} cannot be used for this cache.
* Most applications should respond to this exception by logging a
* warning.
*/
public static HttpResponseCache install(File directory, long maxSize) throws IOException {
HttpResponseCache installed = getInstalled();
if (installed != null) {
// don't close and reopen if an equivalent cache is already installed
DiskLruCache installedCache = installed.delegate.getCache();
if (installedCache.getDirectory().equals(directory)
&& installedCache.getMaxSize() == maxSize
&& !installedCache.isClosed()) {
return installed;
} else {
IoUtils.closeQuietly(installed);
}
}
HttpResponseCache result = new HttpResponseCache(directory, maxSize);
ResponseCache.setDefault(result);
HttpsURLConnection.setDefaultHostnameVerifier(new DefaultHostnameVerifier());
if(!calledSetURLStreamHandlerFactory) {
// The stream handler factory can only be set once, so don't set it again if it's already been set
// This can happen if the application calls install() then delete() then install() again
calledSetURLStreamHandlerFactory = true;
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactoryImpl());
}
return result;
}
示例12: queryDocument
import libcore.io.IoUtils; //导入依赖的package包/类
@Override
public Cursor queryDocument(String docId, String[] projection) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
if (DOC_ID_ROOT.equals(docId)) {
includeDefaultDocument(result);
} else {
// Delegate to real provider
final long token = Binder.clearCallingIdentity();
Cursor cursor = null;
try {
cursor = mDm.query(new Query().setFilterById(Long.parseLong(docId)));
copyNotificationUri(result, cursor);
if (cursor.moveToFirst()) {
includeTransferFromCursor(result, cursor);
}
} finally {
IoUtils.closeQuietly(cursor);
Binder.restoreCallingIdentity(token);
}
}
return result;
}
示例13: queryChildDocuments
import libcore.io.IoUtils; //导入依赖的package包/类
@Override
public Cursor queryChildDocuments(String docId, String[] projection, String sortOrder)
throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
// Delegate to real provider
final long token = Binder.clearCallingIdentity();
Cursor cursor = null;
try {
cursor = mDm.query(new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true)
.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL));
copyNotificationUri(result, cursor);
while (cursor.moveToNext()) {
includeTransferFromCursor(result, cursor);
}
} finally {
IoUtils.closeQuietly(cursor);
Binder.restoreCallingIdentity(token);
}
return result;
}
示例14: queryChildDocumentsForManage
import libcore.io.IoUtils; //导入依赖的package包/类
@Override
public Cursor queryChildDocumentsForManage(
String parentDocumentId, String[] projection, String sortOrder)
throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
// Delegate to real provider
final long token = Binder.clearCallingIdentity();
Cursor cursor = null;
try {
cursor = mDm.query(
new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true));
copyNotificationUri(result, cursor);
while (cursor.moveToNext()) {
includeTransferFromCursor(result, cursor);
}
} finally {
IoUtils.closeQuietly(cursor);
Binder.restoreCallingIdentity(token);
}
return result;
}
示例15: clearBackupState
import libcore.io.IoUtils; //导入依赖的package包/类
private int clearBackupState(boolean closeFile) {
if (backupState == null) {
return TRANSPORT_OK;
}
try {
IoUtils.closeQuietly(backupState.getInputFileDescriptor());
backupState.setInputFileDescriptor(null);
ZipOutputStream outputStream = backupState.getOutputStream();
if (outputStream != null) {
outputStream.closeEntry();
}
if (backupState.getPackageIndex() == configuration.getPackageCount() || closeFile) {
if (outputStream != null) {
outputStream.finish();
outputStream.close();
}
IoUtils.closeQuietly(backupState.getOutputFileDescriptor());
backupState = null;
}
} catch (IOException ex) {
Log.e(TAG, "Error cancelling full backup: ", ex);
return TRANSPORT_ERROR;
}
return TRANSPORT_OK;
}