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