Skip to content

Commit 08b75c7

Browse files
committed
FileInputStream & FileOutputStream源码分析
1 parent 998fd67 commit 08b75c7

File tree

2 files changed

+390
-0
lines changed

2 files changed

+390
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//package org.javacore.io;
2+
//
3+
//import java.io.File;
4+
//import java.io.FileDescriptor;
5+
//import java.io.FileNotFoundException;
6+
//import java.io.IOException;
7+
//import java.io.InputStream;
8+
//import java.nio.channels.FileChannel;
9+
//
10+
//import sun.misc.IoTrace;
11+
//import sun.nio.ch.FileChannelImpl;
12+
//
13+
///**
14+
// * FileInputStream 从文件系统的文件中获取输入字节流。文件取决于主机系统。
15+
// * 比如读取图片等的原始字节流。如果读取字符流,考虑使用 FiLeReader。
16+
// */
17+
//public class SFileInputStream extends InputStream
18+
//{
19+
// /* 文件描述符类---此处用于打开文件的句柄 */
20+
// private final FileDescriptor fd;
21+
//
22+
// /* 引用文件的路径 */
23+
// private final String path;
24+
//
25+
// /* 文件通道,NIO部分 */
26+
// private FileChannel channel = null;
27+
//
28+
// private final Object closeLock = new Object();
29+
// private volatile boolean closed = false;
30+
//
31+
// private static final ThreadLocal<Boolean> runningFinalize =
32+
// new ThreadLocal<>();
33+
//
34+
// private static boolean isRunningFinalize() {
35+
// Boolean val;
36+
// if ((val = runningFinalize.get()) != null)
37+
// return val.booleanValue();
38+
// return false;
39+
// }
40+
//
41+
// /* 通过文件路径名来创建FileInputStream */
42+
// public FileInputStream(String name) throws FileNotFoundException {
43+
// this(name != null ? new File(name) : null);
44+
// }
45+
//
46+
// /* 通过文件来创建FileInputStream */
47+
// public FileInputStream(File file) throws FileNotFoundException {
48+
// String name = (file != null ? file.getPath() : null);
49+
// SecurityManager security = System.getSecurityManager();
50+
// if (security != null) {
51+
// security.checkRead(name);
52+
// }
53+
// if (name == null) {
54+
// throw new NullPointerException();
55+
// }
56+
// if (file.isInvalid()) {
57+
// throw new FileNotFoundException("Invalid file path");
58+
// }
59+
// fd = new FileDescriptor();
60+
// fd.incrementAndGetUseCount();
61+
// this.path = name;
62+
// open(name);
63+
// }
64+
//
65+
// /* 通过文件描述符类来创建FileInputStream */
66+
// public FileInputStream(FileDescriptor fdObj) {
67+
// SecurityManager security = System.getSecurityManager();
68+
// if (fdObj == null) {
69+
// throw new NullPointerException();
70+
// }
71+
// if (security != null) {
72+
// security.checkRead(fdObj);
73+
// }
74+
// fd = fdObj;
75+
// path = null;
76+
// fd.incrementAndGetUseCount();
77+
// }
78+
//
79+
// /* 打开文件,为了下一步读取文件内容。native方法 */
80+
// private native void open(String name) throws FileNotFoundException;
81+
//
82+
// /* 从此输入流中读取一个数据字节 */
83+
// public int read() throws IOException {
84+
// Object traceContext = IoTrace.fileReadBegin(path);
85+
// int b = 0;
86+
// try {
87+
// b = read0();
88+
// } finally {
89+
// IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
90+
// }
91+
// return b;
92+
// }
93+
//
94+
// /* 从此输入流中读取一个数据字节。native方法 */
95+
// private native int read0() throws IOException;
96+
//
97+
// /* 从此输入流中读取多个字节到byte数组中。native方法 */
98+
// private native int readBytes(byte b[], int off, int len) throws IOException;
99+
//
100+
// /* 从此输入流中读取多个字节到byte数组中。 */
101+
// public int read(byte b[]) throws IOException {
102+
// Object traceContext = IoTrace.fileReadBegin(path);
103+
// int bytesRead = 0;
104+
// try {
105+
// bytesRead = readBytes(b, 0, b.length);
106+
// } finally {
107+
// IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
108+
// }
109+
// return bytesRead;
110+
// }
111+
//
112+
// /* 从此输入流中读取最多len个字节到byte数组中。 */
113+
// public int read(byte b[], int off, int len) throws IOException {
114+
// Object traceContext = IoTrace.fileReadBegin(path);
115+
// int bytesRead = 0;
116+
// try {
117+
// bytesRead = readBytes(b, off, len);
118+
// } finally {
119+
// IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
120+
// }
121+
// return bytesRead;
122+
// }
123+
//
124+
//
125+
// public native long skip(long n) throws IOException;
126+
//
127+
// /* 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。 */
128+
// public native int available() throws IOException;
129+
//
130+
// /* 关闭此文件输入流并释放与此流有关的所有系统资源。 */
131+
// public void close() throws IOException {
132+
// synchronized (closeLock) {
133+
// if (closed) {
134+
// return;
135+
// }
136+
// closed = true;
137+
// }
138+
// if (channel != null) {
139+
// fd.decrementAndGetUseCount();
140+
// channel.close();
141+
// }
142+
//
143+
// int useCount = fd.decrementAndGetUseCount();
144+
//
145+
// if ((useCount <= 0) || !isRunningFinalize()) {
146+
// close0();
147+
// }
148+
// }
149+
//
150+
// public final FileDescriptor getFD() throws IOException {
151+
// if (fd != null) return fd;
152+
// throw new IOException();
153+
// }
154+
//
155+
// /* 获取此文件输入流的唯一FileChannel对象 */
156+
// public FileChannel getChannel() {
157+
// synchronized (this) {
158+
// if (channel == null) {
159+
// channel = FileChannelImpl.open(fd, path, true, false, this);
160+
// fd.incrementAndGetUseCount();
161+
// }
162+
// return channel;
163+
// }
164+
// }
165+
//
166+
// private static native void initIDs();
167+
//
168+
// private native void close0() throws IOException;
169+
//
170+
// static {
171+
// initIDs();
172+
// }
173+
//
174+
// protected void finalize() throws IOException {
175+
// if ((fd != null) && (fd != FileDescriptor.in)) {
176+
// runningFinalize.set(Boolean.TRUE);
177+
// try {
178+
// close();
179+
// } finally {
180+
// runningFinalize.set(Boolean.FALSE);
181+
// }
182+
// }
183+
// }
184+
//}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//package org.javacore.io;
2+
//
3+
//import java.io.File;
4+
//import java.io.FileDescriptor;
5+
//import java.io.FileNotFoundException;
6+
//import java.io.IOException;
7+
//import java.io.OutputStream;
8+
//import java.nio.channels.FileChannel;
9+
//
10+
//import sun.misc.IoTrace;
11+
//import sun.nio.ch.FileChannelImpl;
12+
//
13+
///**
14+
// * 文件输入流是用于将数据写入文件或者文件描述符类
15+
// * 比如写入图片等的原始字节流。如果写入字符流,考虑使用 FiLeWriter。
16+
// */
17+
//public class SFileOutputStream extends OutputStream
18+
//{
19+
// /* 文件描述符类---此处用于打开文件的句柄 */
20+
// private final FileDescriptor fd;
21+
//
22+
// /* 引用文件的路径 */
23+
// private final String path;
24+
//
25+
// /* 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 */
26+
// private final boolean append;
27+
//
28+
// /* 关联的FileChannel类,懒加载 */
29+
// private FileChannel channel;
30+
//
31+
// private final Object closeLock = new Object();
32+
// private volatile boolean closed = false;
33+
// private static final ThreadLocal<Boolean> runningFinalize =
34+
// new ThreadLocal<>();
35+
//
36+
// private static boolean isRunningFinalize() {
37+
// Boolean val;
38+
// if ((val = runningFinalize.get()) != null)
39+
// return val.booleanValue();
40+
// return false;
41+
// }
42+
//
43+
// /* 通过文件名创建文件输入流 */
44+
// public FileOutputStream(String name) throws FileNotFoundException {
45+
// this(name != null ? new File(name) : null, false);
46+
// }
47+
//
48+
// /* 通过文件名创建文件输入流,并确定文件写入起始处模式 */
49+
// public FileOutputStream(String name, boolean append)
50+
// throws FileNotFoundException
51+
// {
52+
// this(name != null ? new File(name) : null, append);
53+
// }
54+
//
55+
// /* 通过文件创建文件输入流,默认写入文件的开始处 */
56+
// public FileOutputStream(File file) throws FileNotFoundException {
57+
// this(file, false);
58+
// }
59+
//
60+
// /* 通过文件创建文件输入流,并确定文件写入起始处 */
61+
// public FileOutputStream(File file, boolean append)
62+
// throws FileNotFoundException
63+
// {
64+
// String name = (file != null ? file.getPath() : null);
65+
// SecurityManager security = System.getSecurityManager();
66+
// if (security != null) {
67+
// security.checkWrite(name);
68+
// }
69+
// if (name == null) {
70+
// throw new NullPointerException();
71+
// }
72+
// if (file.isInvalid()) {
73+
// throw new FileNotFoundException("Invalid file path");
74+
// }
75+
// this.fd = new FileDescriptor();
76+
// this.append = append;
77+
// this.path = name;
78+
// fd.incrementAndGetUseCount();
79+
// open(name, append);
80+
// }
81+
//
82+
// /* 通过文件描述符类创建文件输入流 */
83+
// public FileOutputStream(FileDescriptor fdObj) {
84+
// SecurityManager security = System.getSecurityManager();
85+
// if (fdObj == null) {
86+
// throw new NullPointerException();
87+
// }
88+
// if (security != null) {
89+
// security.checkWrite(fdObj);
90+
// }
91+
// this.fd = fdObj;
92+
// this.path = null;
93+
// this.append = false;
94+
//
95+
// fd.incrementAndGetUseCount();
96+
// }
97+
//
98+
// /* 打开文件,并确定文件写入起始处模式 */
99+
// private native void open(String name, boolean append)
100+
// throws FileNotFoundException;
101+
//
102+
// /* 将指定的字节b写入到该文件输入流,并指定文件写入起始处模式 */
103+
// private native void write(int b, boolean append) throws IOException;
104+
//
105+
// /* 将指定的字节b写入到该文件输入流 */
106+
// public void write(int b) throws IOException {
107+
// Object traceContext = IoTrace.fileWriteBegin(path);
108+
// int bytesWritten = 0;
109+
// try {
110+
// write(b, append);
111+
// bytesWritten = 1;
112+
// } finally {
113+
// IoTrace.fileWriteEnd(traceContext, bytesWritten);
114+
// }
115+
// }
116+
//
117+
// /* 将指定的字节数组写入该文件输入流,并指定文件写入起始处模式 */
118+
// private native void writeBytes(byte b[], int off, int len, boolean append)
119+
// throws IOException;
120+
//
121+
// /* 将指定的字节数组b写入该文件输入流 */
122+
// public void write(byte b[]) throws IOException {
123+
// Object traceContext = IoTrace.fileWriteBegin(path);
124+
// int bytesWritten = 0;
125+
// try {
126+
// writeBytes(b, 0, b.length, append);
127+
// bytesWritten = b.length;
128+
// } finally {
129+
// IoTrace.fileWriteEnd(traceContext, bytesWritten);
130+
// }
131+
// }
132+
//
133+
// /* 将指定len长度的字节数组b写入该文件输入流 */
134+
// public void write(byte b[], int off, int len) throws IOException {
135+
// Object traceContext = IoTrace.fileWriteBegin(path);
136+
// int bytesWritten = 0;
137+
// try {
138+
// writeBytes(b, off, len, append);
139+
// bytesWritten = len;
140+
// } finally {
141+
// IoTrace.fileWriteEnd(traceContext, bytesWritten);
142+
// }
143+
// }
144+
//
145+
// /* 关闭此文件输出流并释放与此流有关的所有系统资源 */
146+
// public void close() throws IOException {
147+
// synchronized (closeLock) {
148+
// if (closed) {
149+
// return;
150+
// }
151+
// closed = true;
152+
// }
153+
//
154+
// if (channel != null) {
155+
// fd.decrementAndGetUseCount();
156+
// channel.close();
157+
// }
158+
//
159+
// int useCount = fd.decrementAndGetUseCount();
160+
//
161+
// if ((useCount <= 0) || !isRunningFinalize()) {
162+
// close0();
163+
// }
164+
// }
165+
//
166+
// public final FileDescriptor getFD() throws IOException {
167+
// if (fd != null) return fd;
168+
// throw new IOException();
169+
// }
170+
//
171+
// public FileChannel getChannel() {
172+
// synchronized (this) {
173+
// if (channel == null) {
174+
// channel = FileChannelImpl.open(fd, path, false, true, append, this);
175+
//
176+
// fd.incrementAndGetUseCount();
177+
// }
178+
// return channel;
179+
// }
180+
// }
181+
//
182+
// protected void finalize() throws IOException {
183+
// if (fd != null) {
184+
// if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
185+
// flush();
186+
// } else {
187+
//
188+
// runningFinalize.set(Boolean.TRUE);
189+
// try {
190+
// close();
191+
// } finally {
192+
// runningFinalize.set(Boolean.FALSE);
193+
// }
194+
// }
195+
// }
196+
// }
197+
//
198+
// private native void close0() throws IOException;
199+
//
200+
// private static native void initIDs();
201+
//
202+
// static {
203+
// initIDs();
204+
// }
205+
//
206+
//}

0 commit comments

Comments
 (0)