Search in sources :

Example 86 with BufferedInputStream

use of java.io.BufferedInputStream in project fqrouter by fqrouter.

the class DNSConnection method open.

/**
 * NOTE: old connection should be closed. server must be != null. If
 * server is down or unreachable then NoRouteToHostException
 * (subclass of SocketException) is thrown. If connection is
 * remotely refused then ConnectException (subclass of
 * SocketException) is thrown. If SecurityException is caught then
 * SocketException is thrown. Must be synchronized outside.
 **
 * @since 2.2
 */
public void open(InetAddress server) throws NullPointerException, IOException {
    server.hashCode();
    try {
        Socket socket = new Socket(server, PORT);
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream(), DNSMsgHeader.UDP_PACKET_LEN);
        this.out = socket.getOutputStream();
        this.in = in;
        this.socket = socket;
    } catch (SecurityException e) {
        throw new SocketException("SecurityException: connect(" + server.getHostAddress() + ")");
    }
    this.msgBytes = null;
}
Also used : SocketException(java.net.SocketException) BufferedInputStream(java.io.BufferedInputStream) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 87 with BufferedInputStream

use of java.io.BufferedInputStream in project gitblit by gitblit.

the class FileUtils method copy.

/**
	 * Copies a file or folder (recursively) to a destination folder.
	 *
	 * @param destinationFolder
	 * @param filesOrFolders
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
public static void copy(File destinationFolder, File... filesOrFolders) throws FileNotFoundException, IOException {
    destinationFolder.mkdirs();
    for (File file : filesOrFolders) {
        if (file.isDirectory()) {
            copy(new File(destinationFolder, file.getName()), file.listFiles());
        } else {
            File dFile = new File(destinationFolder, file.getName());
            BufferedInputStream bufin = null;
            FileOutputStream fos = null;
            try {
                bufin = new BufferedInputStream(new FileInputStream(file));
                fos = new FileOutputStream(dFile);
                int len = 8196;
                byte[] buff = new byte[len];
                int n = 0;
                while ((n = bufin.read(buff, 0, len)) != -1) {
                    fos.write(buff, 0, n);
                }
            } finally {
                try {
                    if (bufin != null)
                        bufin.close();
                } catch (Throwable t) {
                }
                try {
                    if (fos != null)
                        fos.close();
                } catch (Throwable t) {
                }
            }
            dFile.setLastModified(file.lastModified());
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 88 with BufferedInputStream

use of java.io.BufferedInputStream in project buck by facebook.

the class XmlUtils method getRootTagName.

/**
     * Returns the name of the root element tag stored in the given file, or null if it can't be
     * determined.
     */
@Nullable
public static String getRootTagName(@NonNull File xmlFile) {
    try (InputStream stream = new BufferedInputStream(new FileInputStream(xmlFile))) {
        XMLInputFactory factory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(stream);
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            if (event == XMLStreamReader.START_ELEMENT) {
                return xmlStreamReader.getLocalName();
            }
        }
    } catch (XMLStreamException | IOException ignored) {
    // Ignored.
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) Nullable(com.android.annotations.Nullable)

Example 89 with BufferedInputStream

use of java.io.BufferedInputStream in project XobotOS by xamarin.

the class FtpURLConnection method connectInternal.

private void connectInternal() throws IOException {
    int port = url.getPort();
    int connectTimeout = getConnectTimeout();
    if (port <= 0) {
        port = FTP_PORT;
    }
    if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) {
        controlSocket = new Socket();
    } else {
        controlSocket = new Socket(currentProxy);
    }
    InetSocketAddress addr = new InetSocketAddress(hostName, port);
    controlSocket.connect(addr, connectTimeout);
    connected = true;
    ctrlOutput = controlSocket.getOutputStream();
    ctrlInput = controlSocket.getInputStream();
    login();
    setType();
    if (!getDoInput()) {
        cd();
    }
    try {
        acceptSocket = new ServerSocket(0);
        dataPort = acceptSocket.getLocalPort();
        /* Cannot set REUSEADDR so we need to send a PORT command */
        port();
        if (connectTimeout == 0) {
            // set timeout rather than zero as before
            connectTimeout = 3000;
        }
        acceptSocket.setSoTimeout(getConnectTimeout());
        if (getDoInput()) {
            getFile();
        } else {
            sendFile();
        }
        dataSocket = acceptSocket.accept();
        dataSocket.setSoTimeout(getReadTimeout());
        acceptSocket.close();
    } catch (InterruptedIOException e) {
        throw new IOException("Could not establish data connection");
    }
    if (getDoInput()) {
        inputStream = new FtpURLInputStream(new BufferedInputStream(dataSocket.getInputStream()), controlSocket);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) BufferedInputStream(java.io.BufferedInputStream) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 90 with BufferedInputStream

use of java.io.BufferedInputStream in project XobotOS by xamarin.

the class HttpResponseCache method get.

@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
    String key = uriToKey(uri);
    DiskLruCache.Snapshot snapshot;
    Entry entry;
    try {
        snapshot = cache.get(key);
        if (snapshot == null) {
            return null;
        }
        entry = new Entry(new BufferedInputStream(snapshot.getInputStream(ENTRY_METADATA)));
    } catch (IOException e) {
        // Give up because the cache cannot be read.
        return null;
    }
    if (!entry.matches(uri, requestMethod, requestHeaders)) {
        snapshot.close();
        return null;
    }
    InputStream body = newBodyInputStream(snapshot);
    return entry.isHttps() ? entry.newSecureCacheResponse(body) : entry.newCacheResponse(body);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DiskLruCache(libcore.io.DiskLruCache) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56