-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryFileOutputStream.java
164 lines (141 loc) · 5.67 KB
/
SentryFileOutputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package io.sentry.instrumentation.file;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.ISpan;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An implementation of {@link java.io.FileOutputStream} that creates a {@link io.sentry.ISpan} for
* writing operation with filename and byte count set as description
*
* <p>Note, that span is started when this OutputStream is instantiated via constructor and finishes
* when the {@link java.io.FileOutputStream#close()} is called.
*/
public final class SentryFileOutputStream extends FileOutputStream {
private final @NotNull FileOutputStream delegate;
private final @NotNull FileIOSpanManager spanManager;
public SentryFileOutputStream(final @Nullable String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false, HubAdapter.getInstance());
}
public SentryFileOutputStream(final @Nullable String name, final boolean append)
throws FileNotFoundException {
this(init(name != null ? new File(name) : null, append, null, HubAdapter.getInstance()));
}
public SentryFileOutputStream(final @Nullable File file) throws FileNotFoundException {
this(file, false, HubAdapter.getInstance());
}
public SentryFileOutputStream(final @Nullable File file, final boolean append)
throws FileNotFoundException {
this(init(file, append, null, HubAdapter.getInstance()));
}
public SentryFileOutputStream(final @NotNull FileDescriptor fdObj) {
this(init(fdObj, null, HubAdapter.getInstance()), fdObj);
}
SentryFileOutputStream(final @Nullable File file, final boolean append, final @NotNull IHub hub)
throws FileNotFoundException {
this(init(file, append, null, hub));
}
private SentryFileOutputStream(
final @NotNull FileOutputStreamInitData data, final @NotNull FileDescriptor fd) {
super(fd);
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
delegate = data.delegate;
}
private SentryFileOutputStream(final @NotNull FileOutputStreamInitData data)
throws FileNotFoundException {
super(getFileDescriptor(data.delegate));
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
delegate = data.delegate;
}
private static FileOutputStreamInitData init(
final @Nullable File file,
final boolean append,
@Nullable FileOutputStream delegate,
@NotNull IHub hub)
throws FileNotFoundException {
final ISpan span = FileIOSpanManager.startSpan(hub, "file.write");
if (delegate == null) {
delegate = new FileOutputStream(file, append);
}
return new FileOutputStreamInitData(
file, append, span, delegate, hub.getOptions().isSendDefaultPii());
}
private static FileOutputStreamInitData init(
final @NotNull FileDescriptor fd, @Nullable FileOutputStream delegate, @NotNull IHub hub) {
final ISpan span = FileIOSpanManager.startSpan(hub, "file.write");
if (delegate == null) {
delegate = new FileOutputStream(fd);
}
return new FileOutputStreamInitData(
null, false, span, delegate, hub.getOptions().isSendDefaultPii());
}
@Override
public void write(final int b) throws IOException {
spanManager.performIO(
() -> {
delegate.write(b);
return 1;
});
}
@Override
public void write(final byte @NotNull [] b) throws IOException {
spanManager.performIO(
() -> {
delegate.write(b);
return b.length;
});
}
@Override
public void write(final byte @NotNull [] b, final int off, final int len) throws IOException {
spanManager.performIO(
() -> {
delegate.write(b, off, len);
return len;
});
}
@Override
public void close() throws IOException {
spanManager.finish(delegate);
}
private static FileDescriptor getFileDescriptor(final @NotNull FileOutputStream stream)
throws FileNotFoundException {
try {
return stream.getFD();
} catch (IOException error) {
throw new FileNotFoundException("No file descriptor");
}
}
public static final class Factory {
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @Nullable String name)
throws FileNotFoundException {
return new SentryFileOutputStream(
init(name != null ? new File(name) : null, false, delegate, HubAdapter.getInstance()));
}
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @Nullable String name, final boolean append)
throws FileNotFoundException {
return new SentryFileOutputStream(
init(name != null ? new File(name) : null, append, delegate, HubAdapter.getInstance()));
}
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @Nullable File file)
throws FileNotFoundException {
return new SentryFileOutputStream(init(file, false, delegate, HubAdapter.getInstance()));
}
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @Nullable File file, final boolean append)
throws FileNotFoundException {
return new SentryFileOutputStream(init(file, append, delegate, HubAdapter.getInstance()));
}
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @NotNull FileDescriptor fdObj) {
return new SentryFileOutputStream(init(fdObj, delegate, HubAdapter.getInstance()), fdObj);
}
}
}