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;
}
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());
}
}
}
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;
}
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);
}
}
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);
}
Aggregations