# IO
# OS I/O
输入操作通常包括两个阶段
- 等待数据 — 等待数据从网络中到达。当所等待数据到达时,它被复制到内核中的某个缓冲区
- 从内核向进程复制数据 — 把数据从内核缓冲区复制到应用进程缓冲区
IO
- synchronous IO
- blocking IO — block for both phases
- non-blocking IO — polling for the first phase, block for the second phase
- IO multiplexing, aka event driven IO —
selectorpoll, likejava.nio.channels.Selector, block for both phases, but one thread for multiple sockets - signal driven IO —
sigaction, return immediately for the first phase。内核在数据到达时向应用进程发送SIGIO信号, blocking for second phase
- asynchronous IO
- asynchronous IO —
aio_read, 内核会在所有操作完成之后向应用进程发送信号, non-blocking for both phases
- asynchronous IO —
- synchronous IO
select— likejava.nio.channels.Selector, 监视一组文件描述符,等待一个或者多个描述符成为就绪状态int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);nfds— the highest-numbered file descriptor in any of the three sets, plus 1fd_set— array based, of sizeFD_SETSIZEwhich defaults to 1024readset、writeset、exceptset— 分别对应读、写、异常条件的描述符集合, three classes of events on the specified set of file descriptors, can beNULL; upon return, each of the file descriptor sets will be cleared of all file descriptors except for those that are ready / exceptional- return — on success, return the number of file descriptors contained in the three returned descriptor sets; 0 on timeout; -1 on error
poll— similar toselect, but with more event types and no size constraint for array*fdsint poll(struct pollfd *fds, unsigned int nfds, int timeout); struct pollfd { int fd; /* file descriptor */ short events; /* requested events */ short revents; /* returned events */ };- structure reuse
// If we detect the event, zero it out so we can reuse the structure if ( fds[0].revents & POLLIN ) fds[0].revents = 0; - return — on success, returns a nonnegative value which is the number of elements in the
pollfdswhosereventsfields have been set to a nonzero value; 0 on timeout; -1 on error
- structure reuse
epoll— event pollint epoll_create(int size); int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);epoll_create()— creates a newepollinstance, thesizeargument is ignored, but must be greater than zeroepoll_ctl— add, modify, or remove entries in the interest list of theepollinstance referred to by the file descriptorepfd. 已注册的描述符在内核中会被维护在一棵红黑树上op,fd— it requests that the operationopbe performed for the target file descriptor,fd
epoll_wait— waits for events on theepollinstance referred to by the file descriptorepfd. The buffer pointed to byeventsis used to return information from the ready list about file descriptors in the interest list that have some events available- return — on success, return the number of file descriptors ready for the requested I/O; 0 on timeout; -1 on error
- mode
- LT, level trigger — default, blocking and non-blocking, after
epoll_wait, 进程可以不立即处理该事件,下次调用epoll_wait()会再次通知进程 - ET, edge trigger — non-blocking, after
epoll_wait, 下次再调用epoll_wait()时不会再得到事件到达的通知
- LT, level trigger — default, blocking and non-blocking, after
epollvspollvsselectepoll进程不需要通过轮询来获得事件完成的描述符epoll只需要将描述符从进程缓冲区向内核缓冲区拷贝一次selectandpoll, 每次调用都需要将全部描述符从应用进程缓冲区复制到内核缓冲区
- number and duration of fd — 需要监控的描述符状态变化多,而且都是非常短暂的,也没有必要使用 epoll。因为 epoll 中的所有描述符都存储在内核中,造成每次需要对描述符的状态改变都需要通过 epoll_ctl() 进行系统调用,频繁系统调用降低效率。并且 epoll 的描述符存储在内核,不容易调试。
- 对多线程编程更友好,一个线程调用了
epoll_wait()另一个线程关闭了同一个描述符也不会产生像select和poll的不确定情况- 如果一个线程对某个描述符调用了
select或者poll,另一个线程关闭了该描述符,会导致调用结果不确定
- 如果一个线程对某个描述符调用了
selecttimeout参数精度为微秒, 更加适用于实时性要求比较高的场景,而poll和epoll为毫秒
# Java IO
Netty 粘包拆包 — distinguish data boundary
- delimiter
DelimiterBasedFrameDecoderLineBasedFrameDecoder
- length
FixedLengthFrameDecoder- length header
- delimiter
java.ioexceptionsjava.io.IOExceptionextendsException—java.io.CharConversionException-java.io.EOFException-java.io.FileNotFoundException-java.io.InterruptedIOException-java.io.ObjectStreamException-java.io.InvalidClassException-java.io.InvalidObjectException-java.io.NotActiveException-java.io.NotSerializableException-java.io.OptionalDataException-java.io.StreamCorruptedException-java.io.WriteAbortedException-java.io.SyncFailedException-java.io.UnsupportedEncodingException-java.io.UTFDataFormatExceptionjava.io.UncheckedIOExceptionextendsRuntimeException
# Console
java.io.Console— synchronizedpublic final class Console extends Object implements Flushable- creation
System.console()
- write
void flush()Console format(String fmt, Object... args)Console printf(String format, Object... args)
- read
String readLine()
String readLine(String fmt, Object... args)char[] readPassword()
char[] readPassword(String fmt, Object... args)
- get underlying stream
Reader reader()PrintWriter writer()
- creation
java.util.Scanner— not synchronizedpublic final class Scanner extends Object implements Iterator<String>, Closeable- constructors
Scanner(File source)Scanner(File source, String charsetName)Scanner(InputStream source)— usesInputStreamReaderbehind the scenesSystem.infor stdin
Scanner(InputStream source, String charsetName)Scanner(Path source)Scanner(Path source, String charsetName)Scanner(String source)Scanner(Readable source)Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));Scanner(ReadableByteChannel source)Scanner(ReadableByteChannel source, String charsetName)
- settings — delimiter, locale, regex
Scanner reset()Scanner useDelimiter(Pattern pattern)Scanner useDelimiter(String pattern)Pattern delimiter()Scanner useLocale(Locale locale)Locale locale()Scanner useRadix(int radix)int radix()
- read
String next()String next(Pattern pattern)String next(String pattern)String nextLine()BigDecimal nextBigDecimal()BigInteger nextBigInteger()BigInteger nextBigInteger(int radix)boolean nextBoolean()byte nextByte()byte nextByte(int radix)double nextDouble()float nextFloat()int nextInt()int nextInt(int radix)long nextLong()long nextLong(int radix)short nextShort()short nextShort(int radix)MatchResult match()— the match result of the last scanning operation- peek
s.hasNext(".*"); // ".*" matches anything, similar to hasNext(), but updates the scanner's internal match variable s.match().group(0);
- peek
- test —
has-prefixed version of read methods,hasNext - find — ignoring delimiters, the scanner returns and advances past the match if found, else returns
nullwith position unchangedString findInLine(Pattern pattern)String findInLine(String pattern)String findWithinHorizon(Pattern pattern, int horizon)— will never search more thanhorizoncode points beyond its current position,horizonignored if it is 0String findWithinHorizon(String pattern, int horizon)
- skip — ignoring delimiters
Scanner skip(Pattern pattern)Scanner skip(String pattern)
IOException ioException()— Returns theIOExceptionlast thrown by this Scanner's underlyingReadable.
- constructors
java.io.BufferedReader— synchronizedpublic class BufferedReader extends ReaderStream<String> lines()String readLine()Readermethods
stdin, stdout, stderr
- see
System - see
InputStream(BufferedInputStream) andPrintStream
- see
# Basic IO Stream
IO streams
- byte streams, byte oriented —
InputStream,OutputStream- deal with bytes —
FilterInputStream,FilterOutputStreamand their derivatives - buffer — use
BufferedInputStreamandBufferedOutputStreamas an intermediate stream
- deal with bytes —
charstreams, two-bytecharvalues (UTF-16 codepoints) oriented —Reader,Writer, can be converted from byte streams, both have a protectedObjectfieldlockfor synchronization- usage — combination, filter streams as wrappers
DataInputStream din = new DataInputStream( new BufferedInputStream( new FileInputStream("employee.dat")));
- byte streams, byte oriented —
IO interfaces
java.io.Closeable— idempotent variant withIOExceptioncompared toAutoCloseablewithExceptionpublic interface Closeable extends AutoCloseableclose()when already closed — no effect if already closed, whereasAutoCloseable::closemay have side effects
java.io.Flushable— write any buffered output to the underlying streampublic interface Flushable { void flush() throws IOException; }Readablepublic interface Readable { public int read(java.nio.CharBuffer cb) throws IOException; }Appendable— to which char sequences and values can be appended, must be implemented by any class whose instances are intended to receive formatted output from ajava.util.Formatterpublic interface Appendable { Appendable append(char c) throws IOException; Appendable append(CharSequence csq) throws IOException; Appendable append(CharSequence csq, int start, int end) throws IOException; }
java.io.InputStreampublic abstract class InputStream extends Object implements Closeable- lifecycle
int available()— an estimate of the number of bytes that can be read (or skipped over) without blockingabstract int read()— block if necessary, read next byte (0 ~ 255),-1if at end, used by some other methods so only one method to implementing when inheriting
int read(byte[] b)
int read(byte[] b, int off, int len)— reads up tolenbytes of data from the offset into byte bufferblong skip(long n)— returns the actual number of bytes skippedvoid close()
- mark — the stream somehow remembers all the bytes read after the call to
markand stands ready to supply those same bytes again if and whenever the methodresetis called, as long as withinreadlimitboolean markSupported()void mark(int readlimit)void reset()
- read all to
String— see stack overflow (opens new window)public String inputStreamToString(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); inputStream.transferTo(bos); return bos.toString("utf-8"); }public long transferTo(OutputStream out)— since JDK 9public long transferTo(OutputStream out) throws IOException { Objects.requireNonNull(out, "out"); long transferred = 0; byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; // 8192 int read; while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) { out.write(buffer, 0, read); transferred += read; } return transferred; }
- lifecycle
java.io.OutputStreampublic abstract class OutputStream extends Object implements Closeable, Flushableabstract void write(int b)— block if necessary
void write(byte[] b)
void write(byte[] b, int off, int len)void flush()void close()— automaticallyflush()before close
java.io.Reader— seeInputStreampublic abstract class Reader extends Object implements Readable, Closeable- lifecycle
boolean ready()abstract int read(char[] cbuf, int off, int len)— 0 ~ 65535 or -1
int read()
int read(char[] cbuf)
int read(CharBuffer target)long skip(long n)abstract void close()
- mark
boolean markSupported()void mark(int readAheadLimit)void reset()
- lifecycle
java.io.Writer— seeOutputStreampublic abstract class Writer extends Object implements Appendable, Closeable, FlushableWriter append(char c)
Writer append(CharSequence csq)
Writer append(CharSequence csq, int start, int end)abstract void write(char[] cbuf, int off, int len)
void write(char[] cbuf)
void write(int c)
void write(String str)
void write(String str, int off, int len)abstract void flush()abstract void close()
convert stream to reader or writer, uses
Charset.defaultCharset()if not specifiedjava.io.InputStreamReader— used byScannerbehind the scenespublic class InputStreamReader extends Reader- constructors
InputStreamReader(InputStream in)InputStreamReader(InputStream in, Charset cs)InputStreamReader(InputStream in, CharsetDecoder dec)InputStreamReader(InputStream in, String charsetName)
String getEncoding()
- constructors
java.io.OutputStreamWriterpublic class OutputStreamWriter extends Writer- constructors
OutputStreamWriter(OutputStream out)OutputStreamWriter(OutputStream out, Charset cs)OutputStreamWriter(OutputStream out, CharsetEncoder enc)OutputStreamWriter(OutputStream out, String charsetName)
String getEncoding()
- constructors
# Filter Stream
filter stream — contains some other stream, which it uses as its basic source or sink of data, possibly transforming the data along the way or providing additional functionality
java.io.FilterInputStreampublic class FilterInputStream extends InputStream- constructor —
protected FilterInputStream(InputStream in)
- constructor —
java.io.FilterOutputStreampublic class FilterOutputStream extends OutputStream- constructor —
FilterOutputStream(OutputStream out)
- constructor —
java.io.FilterReaderpublic abstract class FilterReader extends Readerjava.io.FilterWriterpublic abstract class FilterWriter extends Writer
buffer streams — the ability to buffer the input and to support the mark and reset methods
java.io.BufferedInputStreampublic class BufferedInputStream extends FilterInputStream- constructors
BufferedInputStream(InputStream in)BufferedInputStream(InputStream in, int size)
- constructors
java.io.BufferedOutputStreampublic class BufferedOutputStream extends FilterOutputStream- constructors
BufferedOutputStream(OutputStream out)BufferedOutputStream(OutputStream out, int size)
- constructors
java.io.BufferedReaderpublic class BufferedReader extends Readerjava.io.BufferedWriterpublic class BufferedWriter extends Writer
data input — conversion between bytes from a binary stream and Java data types, big endian
- modified UTF-8 (opens new window) — UTF-8 encoded UTF-16
java.io.DataInputpublic interface DataInputvoid readFully(byte[] b)void readFully(byte[] b, int off, int len)boolean readBoolean()byte readByte()char readChar()double readDouble()float readFloat()int readInt()String readLine()long readLong()short readShort()int readUnsignedByte()— 0 ~ 255int readUnsignedShort()— 0 ~ 65535String readUTF()— modified UTF-8 encoded stringint skipBytes(int n)
java.io.DataOutputpublic interface DataOutputjava.io.DataInputStreampublic class DataInputStream extends FilterInputStream implements DataInput- constructor —
DataInputStream(InputStream in)
- constructor —
java.io.DataOutputStreampublic class DataOutputStream extends FilterOutputStream implements DataOutput- constructor —
DataOutputStream(OutputStream out)
- constructor —
peek — push back stream
java.io.PushbackInputStreampublic class PushbackInputStream extends FilterInputStream- constructors
PushbackInputStream(InputStream in)PushbackInputStream(InputStream in, int size)
- push back
void unread(byte[] b)void unread(byte[] b, int off, int len)void unread(int b)
- constructors
java.io.PushbackReaderpublic class PushbackReader extends FilterReader
# ZIP Streams
inflate and deflate
java.util.zip.InflaterInputStreampublic class InflaterInputStream extends FilterInputStreamjava.util.zip.DeflaterOutputStreampublic class DeflaterOutputStream extends FilterOutputStream
ZIP stream
java.util.zip.ZipInputStreampublic class ZipInputStream extends InflaterInputStreamjava.util.zip.ZipOutputStreampublic class ZipOutputStream extends DeflaterOutputStream
ZIP file system —
FileSystems.newFileSystem(Paths.get(zipname), null)
# Print Stream
print streams — add the ability to print representations of various data values conveniently
- never throws an
IOException— onlycheckError() - auto flush — support auto flush, defaults to
false- for
PrintStream— after a byte array is written, or a\nis written - for
PrintWriter— after the invoke ofprintln,printf, orformat
- for
printf— formats (opens new window)
- never throws an
java.io.PrintStream— print into bytespublic class PrintStream extends FilterOutputStream implements Appendable, Closeable- constructors
PrintStream(File file)PrintStream(File file, String csn)PrintStream(OutputStream out)PrintStream(OutputStream out, boolean autoFlush)PrintStream(OutputStream out, boolean autoFlush, String encoding)PrintStream(String fileName)PrintStream(String fileName, String csn)
printandprintlnmethods —void, supports primitive types,char[]andObjectprintfPrintStream printf(Locale l, String format, Object... args)PrintStream printf(String format, Object... args)
boolean checkError()
- constructors
java.io.PrintWriter— print into text (chars)public class PrintWriter extends Writer- constructors
PrintWriter(File file)PrintWriter(File file, String csn)PrintWriter(OutputStream out)PrintWriter(OutputStream out, boolean autoFlush)PrintWriter(String fileName)PrintWriter(String fileName, String csn)PrintWriter(Writer out)PrintWriter(Writer out, boolean autoFlush)
- methods — see
PrintStream- difference —
PrintStream::writemethods allowintandbyte[]
- difference —
- constructors
# Other Streams
- externally buffered streams — save the data in an internal buffer (byte array, etc.), no effect for
close()and noIOExceptionafterwardsjava.io.ByteArrayInputStreampublic class ByteArrayInputStream extends InputStreamjava.io.ByteArrayOutputStreampublic class ByteArrayOutputStream extends OutputStreamjava.io.StringReader— read fromString:StringReader(String s)public class StringReader extends Readerjava.io.StringWriter— usesStringBufferas bufferpublic class StringWriter extends Writer
# Serialization
transient— mark fields not part of the persistent state, which is skipped in serializationinterface java.io.Serializable— mark only data fields serializable, superclass data or any other class information not included- deserialize fields of classes not
Serializable— initialized using the public or protected no-arg constructor - serialize subclasses whose parents are not
Serializable— serialize the super types only when they have accessible no-arg constructor - serialization and deserialization control — override default read and write behavior, special handling during the serialization and deserialization, by implementing methods below
private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException private void readObjectNoData() throws ObjectStreamException - version ID — used during deserialization to verify that the sender and receiver of a serialized object have loaded classes compatible with serialization,
InvalidClassExceptionif no match- declare explicitly
MODIFIER static final long serialVersionUID = 42L; // private is recommended - generate by default, not recommended — the serialization runtime will calculate a default
serialVersionUIDvalue for that class based on various aspects of the class (fingerprint), which may vary depending on compiler implementations - not applicable to array classes — cannot declare explicitly, and the requirement for matching
serialVersionUIDvalues is waived for array classes - get version ID via CLI —
serialver ClassName - auto conversion when version ID match — for data fields, skip when type is different, ignore additional, set absent to default
- declare explicitly
- write or read with another object
- when writing to stream — implement
writeReplaceMODIFIER Object writeReplace() throws ObjectStreamException; - when reading from stream — implement
readResolveMODIFIER Object readResolve() throws ObjectStreamException;
- when writing to stream — implement
- serial number — when serializing, associate the object a number in encounter order and save or read the object data when first encounter, only save the serial number or read the object reference when encountered afterwards
- file structure
- magic number —
ACED - version number of the object serialization format —
0005for JDK 8 - object sequences
- strings saved in modified UTF-8
- fingerprint stored in class — first 8 bytes of SHA
- more
- magic number —
- deserialize fields of classes not
java.io.Externalizable— complete control over the format and contents of the stream for an object and its superclassespublic interface Externalizable extends Serializable- taking precedence and mechanism — If the object supports
Externalizable, thewriteExternalmethod is called. If the object does not supportExternalizableand does implementSerializable, the object is saved usingObjectOutputStream - no-arg constructor when reconstructing — when reading, creates an object with the no-argument constructor and then calls the
readExternalmethod - use another object — support
writeReplaceandreadResolvemethods void readExternal(ObjectInputStream in) throws IOException, ClassNotFoundExceptionvoid writeExternal(ObjectOutputStream out) throws IOException
- taking precedence and mechanism — If the object supports
interfaces for object streams
interface java.io.ObjectStreamConstants— Constants written into the Object Serialization Streamjava.io.ObjectOutputpublic interface ObjectOutput extends DataOutput, AutoCloseable- inherited methods
void flush()void writeObject(Object obj)
java.io.ObjectInputpublic interface ObjectInput extends DataInput, AutoCloseable- inherited methods
int available()int read()
int read(byte[] b)
int read(byte[] b, int off, int len)Object readObject()long skip(long n)
java.io.ObjectInputStreampublic class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants- constructor —
ObjectInputStream(InputStream in) void defaultReadObject()
- constructor —
java.io.ObjectOutputStreampublic class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants- constructor —
ObjectOutputStream(OutputStream out) void defaultWriteObject()
- constructor —
# Files
# File Classes
java.io.File— an abstract representation of file and directory pathnames, the old school waypublic class File extends Object implements Serializable, Comparable<File>- separators —
System.getProperty("file.separator"),System.getProperty("path.separator")static String separatorstatic char separatorCharstatic String pathSeparatorstatic char pathSeparatorChar
- permissions
boolean canExecute()boolean canRead()boolean canWrite()boolean isHidden()- `boolean setExecutable(boolean executable)
- `boolean setExecutable(boolean executable, boolean ownerOnly)
- `boolean setReadable(boolean readable)
- `boolean setReadable(boolean readable, boolean ownerOnly)
- `boolean setReadOnly()
- `boolean setWritable(boolean writable)
- `boolean setWritable(boolean writable, boolean ownerOnly)
- inherited
int compareTo(File pathname)— lexicographically
- CRUD
static File createTempFile(String prefix, String suffix)static File createTempFile(String prefix, String suffix, File directory)boolean createNewFile()boolean mkdir()boolean mkdirs()boolean delete()void deleteOnExit()boolean exists()boolean renameTo(File dest)
- metadata and list
boolean isDirectory()boolean isFile()long lastModified()boolean setLastModified(long time)long length()String[] list()
String[] list(FilenameFilter filter)File[] listFiles()
File[] listFiles(FileFilter filter)
File[] listFiles(FilenameFilter filter)static File[] listRoots()
- path
File getAbsoluteFile()String getAbsolutePath()File getCanonicalFile()String getCanonicalPath()String getName()String getParent()File getParentFile()String getPath()boolean isAbsolute()String toString()Path toPath()URI toURI()
- space — the partition named by this abstract pathname
long getFreeSpace()long getTotalSpace()long getUsableSpace()
- file filters
java.io.FilenameFilter@FunctionalInterface public interface FilenameFilter { boolean accept(File dir, String name); }java.io.FileFilter@FunctionalInterface public interface FileFilter { boolean accept(File pathname); }
java.io.FileDescriptor— used in file streamspublic final class FileDescriptor extends Object ``
- separators —
java.nio.file.Path— represents a system dependent file path, immutablepublic interface Path extends Comparable<Path>, Iterable<Path>, Watchable- creation
java.nio.file.Pathspublic final class Paths extends Objectstatic Path get(String first, String... more)— joinstatic Path get(URI uri)
File::toPath
- components methods
- relative, absolute, real path methods
File toFile()URI toUri()- inherited methods
- example: three ways to
pwdnew java.io.File(".").getCanonicalPath(); System.getProperty("user.dir"); Paths.get(".").toAbsolutePath().normalize().toString();
- creation
java.nio.file.Files— static methods takePathas arguments, operate its underlying files, usually atomicallypublic final class Files extends Object- limit — some read / write methods are intended for text files of moderate length, use stream methods such as
newInputStreamfor large or binary files - glop pattern — extended syntax, extended
**, see File Operations (The Java™ Tutorials > Essential Classes > Basic I/O) (opens new window), andFileSystem::getPathMatcher(opens new window) readAllBytes,readAllLines, etc.
- limit — some read / write methods are intended for text files of moderate length, use stream methods such as
utility classes
java.nio.file.DirectoryStream— an object to iterate over the entries in a directory, supports only a singleIteratorpublic interface DirectoryStream<T> extends Closeable, Iterable<T>java.nio.file.SimpleFileVisitor— a simple visitor of files with default behavior to visit all files and to re-throw I/O errorspublic class SimpleFileVisitor<T> extends Object implements FileVisitor<T>public interface java.nio.file.FileVisitor<T>- prevent termination by exceptions — override
postVisitDirectoryto returnFileVisitResult.CONTINUEandvisitFileFailedto returnFileVisitResult.SKIP_SUBTREE
java.nio.file.PathMatcher@FunctionalInterface public interface PathMatcher
java.nio.file.FileStore— a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storagepublic abstract class FileStore extends Objectjava.nio.file.FileSystempublic abstract class FileSystem extends Object implements Closeableabstract Path getPath(String first, String... more)abstract Iterable<Path> getRootDirectories()abstract boolean isOpen()abstract boolean isReadOnly()abstract Iterable<FileStore> getFileStores()abstract PathMatcher getPathMatcher(String syntaxAndPattern)abstract String getSeparator()abstract UserPrincipalLookupService getUserPrincipalLookupService()abstract WatchService newWatchService()abstract FileSystemProvider provider()abstract Set<String> supportedFileAttributeViews()- creation —
FileSystems
java.nio.file.FileSystems— factory methods for file systems- initialization — The first invocation of any of the methods defined by this class causes the default
FileSystemProviderto be loaded. The default provider, identified by the URI scheme "file", creates theFileSystemthat provides access to the file systems accessible to the JVM static FileSystem getDefault()static FileSystem getFileSystem(URI uri)static FileSystem newFileSystem(Path path, ClassLoader loader)— constructs a newFileSystemto access the contents of a file as a file system, supports ZIP filesstatic FileSystem newFileSystem(URI uri, Map<String,?> env)static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader)
- initialization — The first invocation of any of the methods defined by this class causes the default
java.nio.file.spi.FileSystemProvider— file system service provider, methods inFilesunder the hoodpublic abstract class FileSystemProvider extends Objectjava.nio.channels.FileChannel— reading, writing, mapping, locking, transferring and manipulating a filepublic abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel- creation
static FileChannel open(Path path, OpenOption... options)static FileChannel open(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)FileInputStream::getChannel,FileOutputStream::getChannel,RandomAccessFile::getChannel
abstract MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)— maps a region of this channel's file directly into memory, recommended only for large files- lock — lock the given region of this channel's file
FileLock lock()— blocks, equivalent tolock(0L, Long.MAX_VALUE, false)
abstract FileLock lock(long position, long size, boolean shared)FileLock tryLock()—nullif not available, equivalent totryLock(0L, Long.MAX_VALUE, false)
abstract FileLock tryLock(long position, long size, boolean shared)
- AIO —
java.nio.channels.AsynchronousFileChannel
- creation
java.nio.channels.FileLock— a lock on a region of a file, on behalf of the JVMpublic abstract class FileLock extends Object implements AutoCloseable- region is fixed — the region stays fixed, the file can have uncovered portion or can grow beyond the region
boolean overlaps(long position, long size)long position()long size()
- lock on behalf of the JVM — not for multithreading, but for multiprocessing
- shared lock — allow other programs to acquire overlapping shared locks while not allowing exclusive locks
sharedfor read, exclusive for write —trueto request a shared lock, in which case this channel must be open for reading (and possibly writing);falseto request an exclusive lock, in which case this channel must be open for writing (and possibly reading)
- OS dependent
- lock support — On some systems, file locking is merely advisory
- shared support — a request for a shared lock is automatically converted into a request for an exclusive lock if not supported
- memory map support — on some systems, you cannot simultaneously lock a file and map it into memory
- avoid multiple channels on the same locked file — on some systems, closing a channel releases all locks on the underlying file
- avoid locking files on a networked file system
abstract void release()
void close()
- region is fixed — the region stays fixed, the file can have uncovered portion or can grow beyond the region
# File Options and Attributes
options
interface java.nio.file.OpenOption— mark interfaceinterface java.nio.file.CopyOption— mark interfacejava.nio.file.LinkOption.NOFOLLOW_LINKS— Do not follow symbolic linkspublic enum LinkOption extends Enum<LinkOption> implements OpenOption, CopyOptionjava.nio.file.StandardOpenOptionpublic enum StandardOpenOption extends Enum<StandardOpenOption> implements OpenOptionREADCREATECREATE_NEWDELETE_ON_CLOSESPARSE— a hint to the file system that this file will be sparseSYNC,DSYNCWRITEAPPENDTRUNCATE_EXISTING
java.nio.file.StandardCopyOptionpublic enum StandardCopyOption extends Enum<StandardCopyOption> implements CopyOptionATOMIC_MOVECOPY_ATTRIBUTESREPLACE_EXISTING
java.nio.file.FileVisitOption.FOLLOW_LINKS— Follow symbolic links, neitherOpenOptionnorCopyOption
file attributes —
java.nio.file.attributejava.nio.file.attribute.UserPrincipal— a Principal representing an identity used to determine access rights to objects in a file systempublic interface UserPrincipal extends Principalinterface java.nio.file.attribute.BasicFileAttributes— basic file attributes, including times, file or dir or link, size, file key- file key — an object of some class, specific to the file system, that may or may not uniquely identify a file
- read attributes —
Files::readAttributes - sub-interfaces
java.nio.file.attribute.DosFileAttributesjava.nio.file.attribute.PosixFileAttributes
java.nio.file.attribute.FileTime— a file's time stamp attributepublic final class FileTime extends Object implements Comparable<FileTime>
# File Stream
Use new classes in java.nio like Files, Paths instead when possible.
file streams
java.io.FileInputStreampublic class FileInputStream extends InputStream- constructors
FileInputStream(File file)FileInputStream(FileDescriptor fdObj)FileInputStream(String name)
- methods beyond
InputStreamprotected void finalize()— ensures that the close method of this file input stream is called when there are no more references to itFileChannel getChannel()FileDescriptor getFD()
- constructors
java.io.FileOutputStreampublic class FileOutputStream extends OutputStream- constructors
FileOutputStream(File file)FileOutputStream(File file, boolean append)FileOutputStream(FileDescriptor fdObj)FileOutputStream(String name)FileOutputStream(String name, boolean append)
- see
FileInputStream
- constructors
- print streams
Scanner,BufferedReaderjava.io.RandomAccessFile— both reading and writing to a random access file, which has a file pointer, suitable for small and moderate filespublic class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable- mode —
"r","rw","rws"(file content or metadata synchronized with storage), or"rwd"(only file content synchronized) - constructors
RandomAccessFile(File file, String mode)RandomAccessFile(String name, String mode)
- file info
FileChannel getChannel()FileDescriptor getFD()long length()
- file pointer — cursor for read / write
long getFilePointer()void seek(long pos)— Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.void setLength(long newLength)int skipBytes(int n)
- read
int read()int read(byte[] b)int read(byte[] b, int off, int len)DataInputmethods
- write —
DataOutputmethods
- mode —
char based file streams — default encoding as
InputStreamReader,OutputStreamWriterjava.io.FileReaderpublic class FileReader extends InputStreamReader- constructors
FileReader(File file)FileReader(FileDescriptor fd)FileReader(String fileName)
- constructors
java.io.FileWriterpublic class FileWriter extends OutputStreamWriter- constructors
FileWriter(File file)FileWriter(File file, boolean append)FileWriter(FileDescriptor fd)FileWriter(String fileName)FileWriter(String fileName, boolean append)
- constructors
- print streams
# NIO
- file related — see File Classes
# NIO Buffers
java.nioByteOrderBuffer— a container for a fixed amount of data of a specific primitive typeByteBuffer— can be direct, can be mapped to memory, content can be heterogeneous or homogeneous, big-endian or little-endianMappedByteBuffer
CharBufferDoubleBuffer,FloatBuffer,IntBuffer,LongBuffer,ShortBuffer
- runtime exceptions
java.nio.BufferOverflowExceptionjava.nio.BufferUnderflowExceptionjava.lang.IllegalStateExceptionjava.nio.InvalidMarkException
java.lang.UnsupportedOperationExceptionjava.nio.ReadOnlyBufferException
java.nio.ByteOrderpublic final class ByteOrder extends Objectstatic ByteOrder BIG_ENDIANstatic ByteOrder LITTLE_ENDIANstatic ByteOrder nativeOrder()— the native byte order of the underlying platform- for buffers —
ByteBuffer::order
java.nio.Buffer— finite sequence of elements, not thread-safepublic abstract class Buffer extends Object- underlying array
abstract Object array()— Returns the array that backs this buffer (optional operation)abstract int arrayOffset()— Returns the offset within this buffer's backing array of the first element of the buffer (optional operation)abstract boolean hasArray()
- indices
int capacity()int limit()— the index of the first element that should not be read or writtenint position()— the index of the next element to be read or written- relative operations — from position
- absolute operations — from explicit index
- change indices
Buffer clear()— forputor related, sets the limit to the capacity and the position to zeroBuffer flip()— forgetor related, sets the limit to the current position and then sets the position to zeroBuffer rewind()— for re-read, sets the position to zeroBuffer limit(int newLimit)Buffer position(int newPosition)- mark and reset
Buffer mark()— set the current as the index to which its position will be set whenreset(), otherwiseInvalidMarkExceptionBuffer reset()
- remaining
int remaining()— from position to limitboolean hasRemaining()
- attributes
abstract boolean isDirect()abstract boolean isReadOnly()
- underlying array
java.nio.ByteBuffer— byte buffer with absolute and relative, bulk and not bulkgetandput, not bulkgetandputwith other types, as well as views and other manipulatingpublic abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer>- view buffers — another buffer whose content is backed by the byte buffer, changes on either one will be reflected on both
- get and put methods — content can be heterogeneous or homogeneous, big-endian or little-endian
- creation
static ByteBuffer allocate(int capacity)static ByteBuffer allocateDirect(int capacity)— direct buffer, which uses a subclass ofjava.nio.MappedByteBufferstatic ByteBuffer wrap(byte[] array)static ByteBuffer wrap(byte[] array, int offset, int length)
- manipulate
abstract ByteBuffer compact()abstract ByteBuffer duplicate()abstract ByteBuffer slice()
java.nio.MappedByteBuffer— a direct byte buffer whose content is a memory-mapped region of a filepublic abstract class MappedByteBuffer extends ByteBufferFileChannel::mapByteBuffer::allocateDirect— return type isByteBufferbut actual type is a subclass ofMappedByteBuffer
- attributes
- direct buffers
- no intermediate buffer — JVM will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations
- higher overhead — have somewhat higher allocation and deallocation costs than non-direct buffers
- gc problems — the contents of direct buffers may reside outside of the normal garbage-collected heap
- usage — best to allocate direct buffers only when they yield a measurable gain in program performance
- creation —
ByteBuffer::allocateDirect,FileChannel::map, view buffers on direct buffers
abstract boolean isDirect()abstract boolean isReadOnly()
- direct buffers
java.nio.CharBufferpublic abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>, Appendable, CharSequence, Readablejava.nio.ShortBuffer,java.nio.LongBuffer,java.nio.IntBuffer,java.nio.FloatBuffer,java.nio.DoubleBufferpublic abstract class DoubleBuffer extends Buffer implements Comparable<DoubleBuffer>
# NIO Channels
exceptions in
java.nio.channelsIOExceptionjava.nio.channels.ClosedChannelExceptionjava.nio.channels.AsynchronousCloseExceptionjava.nio.channels.ClosedByInterruptException
java.nio.channels.FileLockInterruptionExceptionjava.nio.channels.InterruptedByTimeoutException
RuntimeExceptionIllegalArgumentException—java.nio.channels.IllegalChannelGroupException-java.nio.channels.IllegalSelectorException-java.nio.channels.UnresolvedAddressException-java.nio.channels.UnsupportedAddressTypeExceptionIllegalStateException—java.nio.channels.AcceptPendingException-java.nio.channels.AlreadyBoundException-java.nio.channels.AlreadyConnectedException-java.nio.channels.CancelledKeyException-java.nio.channels.ClosedSelectorException-java.nio.channels.ConnectionPendingException-java.nio.channels.IllegalBlockingModeException-java.nio.channels.NoConnectionPendingException-java.nio.channels.NonReadableChannelException-java.nio.channels.NonWritableChannelException-java.nio.channels.NotYetBoundException-java.nio.channels.NotYetConnectedException-java.nio.channels.OverlappingFileLockException-java.nio.channels.ReadPendingException-java.nio.channels.ShutdownChannelGroupException-java.nio.channels.WritePendingException
conversion between stream and channel — constructors of stream based classes and
java.nio.channels.Channelsmethods- channel to stream
Scanner(ReadableByteChannel source)
Scanner(ReadableByteChannel source, String charsetName)Channels::newOutputStream- more
- stream to channel
Channels::newChannelgetChannelmethods in stream based classes
- channel to stream
java.nio.channels.Pipe— a pair of channels that implements a unidirectional pipe
# Channel Interfaces
java.nio.channelsChannel- read and write channels
AsynchronousChannel— supports asynchronous I/O operationsNetworkChannel— to a network socket
Channels— utility methods for channel/stream interoperation
java.nio.channels.Channel— a nexus for I/O operationspublic interface Channel extends Closeablevoid close()boolean isOpen()— avoidClosedChannelException
read and write channels
java.nio.channels.ReadableByteChannel— only one read operation upon a readable channel may be in progress at any given timeint read(ByteBuffer dst)— read into given bufferjava.nio.channels.ScatteringByteChannellong read(ByteBuffer[] dsts)long read(ByteBuffer[] dsts, int offset, int length)
java.nio.channels.WritableByteChannel— only one write operation upon a writable channel may be in progress at any given timeint write(ByteBuffer src)java.nio.channels.GatheringByteChannel— write version ofScatteringByteChannel
java.nio.channels.ByteChannel— read and write bytespublic interface ByteChannel extends ReadableByteChannel, WritableByteChanneljava.nio.channels.SeekableByteChannel— a byte channel that maintains a current position and allows the position to be changed
java.nio.channels.InterruptibleChannel— a channel that can be asynchronously closed and interrupted
network channels
java.nio.channels.NetworkChannel—NetworkChannel bind(SocketAddress local)java.nio.channels.MulticastChannel— a network channel that supports Internet Protocol (IP) multicasting
async channels —
java.nio.channels.AsynchronousChanneljava.nio.channels.AsynchronousByteChannel- read
Future<Integer> read(ByteBuffer dst)<A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer,? super A> handler)
- write
Future<Integer> write(ByteBuffer src)<A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer,? super A> handler)
java.nio.channels.CompletionHandler— callback whencompletedorfailedjava.nio.channels.AsynchronousSocketChannelpublic abstract class AsynchronousSocketChannel implements AsynchronousByteChannel, NetworkChannel
- read
java.nio.channels.AsynchronousServerSocketChannelpublic abstract class AsynchronousServerSocketChannel implements AsynchronousChannel, NetworkChannelAsynchronousFileChannel
# Selector
java.nio.channels.spi.AbstractInterruptibleChannel— base implementation class for interruptible channelspublic abstract class AbstractInterruptibleChannel implements Channel, InterruptibleChanneljava.nio.channels.SelectableChannel— a channel that can be multiplexed via aSelectorpublic abstract class SelectableChannel extends AbstractInterruptibleChannel implements Channel- blocking mode or in non-blocking mode — defaults to blocking mode, must be placed into non-blocking mode before being registered and when registered
abstract SelectableChannel configureBlocking(boolean block)abstract boolean isBlocking()
- register — one or more selectors, at most once for each
SelectionKey register(Selector sel, int ops)abstract SelectionKey register(Selector sel, int ops, Object att)abstract boolean isRegistered()abstract SelectionKey keyFor(Selector sel)abstract int validOps()
- deregister —
SelectionKey::cancel,close()or interrupted
- blocking mode or in non-blocking mode — defaults to blocking mode, must be placed into non-blocking mode before being registered and when registered
java.nio.channels.Selector— a multiplexer ofSelectableChannelobjectspublic abstract class Selector implements Closeable- creation
static Selector open()— created bySelectorProvider::openSelectorabstract boolean isOpen()abstract SelectorProvider provider()
- registration key sets
abstract Set<SelectionKey> keys()— channel registrationsabstract Set<SelectionKey> selectedKeys()— keys such that each key's channel was detected to be ready for at least one of the operations identified in the key's interest set during a prior selection operation; keys are removed by theSetremoval methods- cancelled-key set — the set of keys that have been cancelled (
SelectableChannel::closeorSelectionKey::cancel) but whose channels have not yet been deregistered, not directly accessible; removed from the key set during selection operations
- selection — during which keys may be added to and removed from a selector's selected-key set and may be removed from its key and cancelled-key sets
- step
- empty cancelled-key set, itself and from key set
- OS queried for an update as to the readiness of each remaining channel, if ready, add to selected-key set and its ready-operation set overwritten if newly add otherwise merged
- keys added to the cancelled-key set during the process are as step 1
abstract int select()— blockingabstract int select(long timeout)abstract int selectNow()— non-blocking, clearswakeup()abstract Selector wakeup()— blockedselectwill return immediately, or nextselectwill return immediately if none currently; also invoked afterThread::interrupt
- step
- concurrency — thread-safe, but not the key sets
- creation
java.nio.channels.SelectionKey— a token representing the registration of aSelectableChannelwith aSelectorpublic abstract class SelectionKey- operation bit vector, support depends on the underlying channel
static int OP_ACCEPT— operation-set bit for socket-accept operationsstatic int OP_CONNECT— operation-set bit for socket-connect operationsstatic int OP_READstatic int OP_WRITE
- cancel
abstract void cancel()abstract boolean isValid()
abstract int interestOps()— the interest set, which operation categories will be tested for readinessabstract SelectionKey interestOps(int ops)— set to given value
abstract int readyOps()— the ready set, the operation categories for which the key's channel has been detected to be ready by the key's selectorboolean isAcceptable()boolean isConnectable()boolean isReadable()boolean isWritable()
- binding
abstract SelectableChannel channel()abstract Selector selector()
- concurrency — thread-safe
- operation bit vector, support depends on the underlying channel
Reactor 模型 — 一个线程 Thread 使用一个选择器 Selector 通过轮询的方式去监听多个通道 Channel 上的事件,从而让一个线程就可以处理多个事件
while (true) { selector.select(); Set<SelectionKey> keys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = keys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isAcceptable()) { ServerSocketChannel ssChannel1 = (ServerSocketChannel) key.channel(); // 服务器会为每个新连接创建一个 SocketChannel SocketChannel sChannel = ssChannel1.accept(); sChannel.configureBlocking(false); // 这个新连接主要用于从客户端读取数据 sChannel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { SocketChannel sChannel = (SocketChannel) key.channel(); System.out.println(readDataFromSocketChannel(sChannel)); sChannel.close(); } keyIterator.remove(); } }
# Channel Implementations
FileChannel— see File Classes, extendsAbstractInterruptibleChannelbut notAbstractSelectableChanneljava.nio.channels.SocketChannel— likeSocket, but a selectable channelpublic abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel- creation
static SocketChannel open()static SocketChannel open(SocketAddress remote)
- creation
java.nio.channels.DatagramChannel— likeDatagramSocket, but a selectable channelpublic abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, MulticastChanneljava.nio.channels.ServerSocketChannel— likeServerSocket, but a selectable channelpublic abstract class ServerSocketChannel extends AbstractSelectableChannel implements NetworkChannelmore
# Network
java.net— networking like working with files- low level API
- addresses —
Inet4Address,Inet6Address,InetSocketAddress - sockets
Socket— a TCP clientServerSocket— a TCP serverDatagramSocket— a UDP endpoint API and is used to send and receiveDatagramPacketMulticastSocket— a subclass ofDatagramSocketused when dealing with multicast groups
- interfaces
NetworkInterface— a network interface (e.g. ethernet connection or PPP endpoint) made up of a name, and a list of IP addresses assigned to this interface
- addresses —
- high level API
- URI
- URL
- connections
- low level API
network exceptions
IOExceptionjava.net.HttpRetryExceptionjava.io.InterruptedIOExceptionjava.net.SocketTimeoutException
java.net.MalformedURLExceptionjava.net.ProtocolExceptionjava.net.SocketExceptionjava.net.BindExceptionjava.net.ConnectExceptionjava.net.NoRouteToHostExceptionjava.net.PortUnreachableException
java.net.UnknownHostException—String hostjava.net.UnknownServiceException
java.net.URISyntaxException
# Address
java.net.InetAddresspublic class InetAddress extends Object implements Serializable- address types — unicast, multicast
- IP address scope
- Link-local addresses — designed to be used for addressing on a single link for purposes such as auto-address configuration, neighbor discovery, or when no routers are present.
- Site-local addresses — designed to be used for addressing inside of a site without the need for a global prefix.
- Global addresses — unique across the internet.
- host name resolution — a combination of local configuration and network naming services such as, DNS and NIS
- caching —
InetAddressstores successful as well as unsuccessful host name resolutions- property
networkaddress.cache.ttl— TTL for successful name lookups - property
networkaddress.cache.negative.ttl— TTL for unsuccessful name lookups
- property
- creation
static InetAddress[] getAllByName(String host)static InetAddress getByAddress(byte[] addr)static InetAddress getByAddress(String host, byte[] addr)static InetAddress getByName(String host)static InetAddress getLocalHost()static InetAddress getLoopbackAddress()
java.net.Inet4Addresspublic final class Inet4Address extends InetAddressjava.net.Inet6Addresspublic final class Inet6Address extends InetAddressjava.net.InetSocketAddress— IP Socket Address (IP address + port number), can also be a pair (hostname + port number)public class InetSocketAddress extends SocketAddress- wildcard address — means "any" and can only be used for
bindoperations, created withnullor omittedInetAddress java.net.SocketAddress— for extendingpublic abstract class SocketAddress extends Object implements Serializable
- wildcard address — means "any" and can only be used for
# Sockets
java.net.SocketImplFactoryinterface SocketImplFactory { SocketImpl createSocketImpl(); }java.net.SocketImpl— a common superclass for socket implementationspublic abstract class SocketImpl extends Object implements SocketOptionsinterface java.net.SocketOptions— socket options and get/set methodsjava.net.Socket— TCP clientpublic class Socket extends Object implements Closeable- use — read/write is blocking and can not be interrupted
SocketChannel getChannel()— a socket will have a channel iff the channel itself was created via theSocketChannel::openorServerSocketChannel::acceptmethodsInputStream getInputStream()OutputStream getOutputStream()
- timeout
int getSoTimeout()void setSoTimeout(int timeout)
- connect
- blocking constructors
void connect(SocketAddress endpoint)— blocking and cannot be interruptedvoid connect(SocketAddress endpoint, int timeout)boolean isConnected()
- half-close — one end of a socket connection to terminate its output while still receiving data from the other end
void shutdownInput()void shutdownOutput()boolean isInputShutdown()boolean isOutputShutdown()
- implementation — defaults to an instance of package-visible class inheriting
SocketImplstatic void setSocketImplFactory(SocketImplFactory fac)
- more
- use — read/write is blocking and can not be interrupted
java.net.ServerSocket— TCP serverpublic class ServerSocket extends Object implements Closeable- similar methods in
Socket Socket accept()— wait indefinitely until a client connects to that port and then return the connection asSocketvoid setSoTimeout(int timeout)- bind to local port
- constructors
void bind(SocketAddress endpoint)void bind(SocketAddress endpoint, int backlog)
- similar methods in
# URI and URL
java.net.URI— uniform resource identifiers, to parsing, stringify, componentize, and processpublic final class URI extends Object implements Comparable<URI>, SerializableURL toURL()
encoder and decoder — for
application/x-www-form-urlencodedjava.net.URLEncoder—static String encode(String s, String enc)java.net.URLDecoder—static String decode(String s, String enc)
java.net.URLStreamHandler— the common superclass for all stream protocol handlers, for making a connection for a particular protocol typepublic abstract class URLStreamHandler extends Object- cache — automatically loaded when first encounter, and stored in a hash table
- protected methods for interacting with
URLand open connection
java.net.URL— uniform resource locators, can open connections (locate resource), a special kind of URI, which is not URN (uniform resource name); supports common protocols (schemas) andjar:public final class URL extends Object implements Serializable- URL escaping — not handled, it is the responsibility of the caller to encode and decode, recommended to use
URIto manage - creation
- constructors — take string URL, string URL components, or
URL, and optionallyURLStreamHandleras arguments
- constructors — take string URL, string URL components, or
- URL component get methods
- use
URLConnection openConnection()— usesURLStreamHandler::openConnection, establishes connection only afterURLConnection::connectorURLConnection::getInputStream, etc.URLConnection openConnection(Proxy proxy)InputStream openStream()—openConnection().getInputStream()Object getContent()—openConnection().getContent(), see blow
Object getContent(Class[] classes)
- conversion
String toExternalForm()— uses underlyingURLStreamHandler::toExternalFormString toString()URI toURI()
- URL escaping — not handled, it is the responsibility of the caller to encode and decode, recommended to use
java.net.URLConnection— URL connectionpublic abstract class URLConnection extends Object- establish connections
abstract void connect()- get methods
- get methods — get settings and results, some will open connections implicitly
static void setContentHandlerFactory(ContentHandlerFactory fac)Object getContent()— not very useful,sun.net.www.content.<contentType>is used if no custom content handler byContentHandlerFactory
Object getContent(Class[] classes)—nullif allClass::isInstancefailsInputStream getInputStream()- more
- set methods
void addRequestProperty(String key, String value)void setDoOutput(boolean dooutput)— setdoOutput(defaults tofalse) field totrueto write to the URL connectiondoInputdefaults totrue
OutputStream getOutputStream()- set methods for applets only
- more
java.net.JarURLConnection— use cast fromURLConnectionfor creation, for URLs likejar:<url>!/{entry}, for examplejar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class jar:file:/home/duke/duke.jar!/java.net.HttpURLConnection— use cast fromURLConnectionfor creationpublic abstract class HttpURLConnection extends URLConnectionstatic intHTTP status codesInputStream getErrorStream()— 404 will throwFileNotFoundException, but response data can be also useful- cookies — see HTTP Cookie
- establish connections
# HTTP Cookie
hierarchy of HTTP cookie classes
use CookieHandler <------- HttpURLConnection ^ | impl | use CookieManager -------> CookiePolicy | use |--------> HttpCookie | ^ | | use | use | |--------> CookieStore ^ | impl | Internal in-memory implementationjava.net.CookieHandler— provides a callback mechanism to hook up a HTTP state management policy implementation into the HTTP protocol handlerpublic abstract class CookieHandler extends Objectstatic void setDefault(CookieHandler cHandler)
java.net.CookieManagerpublic class CookieManager extends CookieHandler- creation
CookieManager(CookieStore store, CookiePolicy cookiePolicy)—nullparameters means default value
- creation
interface java.net.CookieStore— store and retrieve cookies, and remove when expiredinterface java.net.CookiePolicy- predefined
static CookiePolicy ACCEPT_ALLstatic CookiePolicy ACCEPT_NONEstatic CookiePolicy ACCEPT_ORIGINAL_SERVER
boolean shouldAccept(URI uri, HttpCookie cookie)
- predefined
java.net.HttpCookie— key-value pair with information likeisHttpOnly(),getMaxAge()