Skip to content

Commit 82aa3ea

Browse files
committed
Replace shadowed android.system with custom androidx.system package
So that we don't shadow framework classes in our binary. Update buffer position in OsCompat.write (fixed in Lollipop MR1). Synthesize InterruptedIOException in Kitkat like in Lollipop. OsCompat never throws EINTR errno.
1 parent e1d1037 commit 82aa3ea

File tree

16 files changed

+320
-85
lines changed

16 files changed

+320
-85
lines changed

config/checkstyle/checkstyle.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,4 @@ page at http://checkstyle.sourceforge.net/config.html -->
154154
<module name="UpperEll" />
155155
</module>
156156

157-
<!-- Excludes android.system polyfill. -->
158-
<!-- See https://checkstyle.org/config_filefilters.html -->
159-
<module name="BeforeExecutionExclusionFileFilter">
160-
<property name="fileNamePattern" value=".*[\\/]java[\\/]android[\\/]system[\\/].*$" />
161-
</module>
162-
163157
</module>

libcore/src/main/java/libcore/io/Os.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.nio.ByteBuffer;
2020

2121
public interface Os {
22-
public String strerror(int errno);
23-
24-
public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException;
22+
String strerror(int errno);
23+
int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException;
2524
}

os-compat/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 31
5+
defaultConfig {
6+
minSdkVersion 19
7+
targetSdkVersion 31
8+
}
9+
androidResources {
10+
namespace = "androidx.system"
11+
}
12+
buildFeatures {
13+
aidl = false
14+
androidResources = false
15+
buildConfig = false
16+
}
17+
}
18+
19+
dependencies {
20+
compileOnly rootProject.fileTree("thirdparty/androidx/annotation/1.3.0/annotation-1.3.0.jar")
21+
compileOnly project(':libcore')
22+
}
23+
24+
apply from: "$project.rootDir/config/android-checkstyle.gradle"

server/src/main/java/android/system/ErrnoException.java renamed to os-compat/src/main/java/androidx/system/ErrnoException.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package android.system;
18-
19-
import android.annotation.TargetApi;
16+
package androidx.system;
2017

2118
import java.io.IOException;
2219
import java.net.SocketException;
2320

24-
import libcore.io.Libcore;
25-
2621
/**
27-
* A checked exception thrown when {@link Os} methods fail. This exception contains the native
28-
* errno value, for comparison against the constants in {@link OsConstants}, should sophisticated
22+
* A checked exception thrown when {@link OsCompat} methods fail. This exception contains the native
23+
* errno value, for comparison against the constants in {@link OsConstantsCompat}, should sophisticated
2924
* callers need to adjust their behavior based on the exact failure.
3025
*/
31-
@SuppressWarnings("unused")
32-
@TargetApi(21)
3326
public final class ErrnoException extends Exception {
3427
private final String functionName;
35-
36-
/**
37-
* The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
38-
*/
39-
public final int errno;
28+
private final int errno;
4029

4130
/**
4231
* Constructs an instance with the given function name and errno value.
@@ -55,17 +44,24 @@ public ErrnoException(String functionName, int errno, Throwable cause) {
5544
this.errno = errno;
5645
}
5746

47+
/**
48+
* The errno value, for comparison with the {@code E} constants in {@link OsConstantsCompat}.
49+
*/
50+
public int getErrno() {
51+
return errno;
52+
}
53+
5854
/**
5955
* Converts the stashed function name and errno value to a human-readable string.
6056
* We do this here rather than in the constructor so that callers only pay for
6157
* this if they need it.
6258
*/
6359
@Override public String getMessage() {
64-
String errnoName = OsConstants.errnoName(errno);
60+
String errnoName = OsConstantsCompat.errnoName(errno);
6561
if (errnoName == null) {
6662
errnoName = "errno " + errno;
6763
}
68-
String description = Libcore.os.strerror(errno);
64+
String description = OsCompat.strerror(errno);
6965
return functionName + " failed: " + errnoName + " (" + description + ")";
7066
}
7167

@@ -77,9 +73,7 @@ public ErrnoException(String functionName, int errno, Throwable cause) {
7773
* {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
7874
*/
7975
public IOException rethrowAsIOException() throws IOException {
80-
IOException newException = new IOException(getMessage());
81-
newException.initCause(this);
82-
throw newException;
76+
throw new IOException(getMessage(), this);
8377
}
8478

8579
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.system;
17+
18+
import java.io.FileDescriptor;
19+
import java.io.InterruptedIOException;
20+
import java.nio.ByteBuffer;
21+
22+
interface Os {
23+
String strerror(int errno);
24+
25+
int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException;
26+
27+
// https://android.googlesource.com/platform/libcore/+/lollipop-mr1-release/luni/src/main/java/libcore/io/Posix.java#253
28+
static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) {
29+
if (bytesReadOrWritten > 0) {
30+
buffer.position(bytesReadOrWritten + originalPosition);
31+
}
32+
}
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.system;
17+
18+
import androidx.annotation.RequiresApi;
19+
20+
import java.io.FileDescriptor;
21+
import java.io.InterruptedIOException;
22+
import java.nio.ByteBuffer;
23+
24+
import static android.os.Build.VERSION.SDK_INT;
25+
import static androidx.system.Os.maybeUpdateBufferPosition;
26+
27+
@RequiresApi(21)
28+
final class OsApi21 implements Os {
29+
@Override
30+
public String strerror(int errno) {
31+
return android.system.Os.strerror(errno);
32+
}
33+
34+
@Override
35+
public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
36+
try {
37+
final int position = buffer.position();
38+
final int bytesWritten = android.system.Os.write(fd, buffer);
39+
if (SDK_INT < 22) {
40+
maybeUpdateBufferPosition(buffer, position, bytesWritten);
41+
}
42+
return bytesWritten;
43+
} catch (android.system.ErrnoException e) {
44+
throw new ErrnoException("write", e.errno);
45+
}
46+
}
47+
}

server/src/main/java/android/system/Os.java renamed to os-compat/src/main/java/androidx/system/OsCompat.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,46 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package android.system;
18-
19-
import android.annotation.TargetApi;
16+
package androidx.system;
2017

2118
import java.io.FileDescriptor;
2219
import java.io.InterruptedIOException;
23-
import java.net.InetAddress;
24-
import java.net.InetSocketAddress;
25-
import java.net.SocketAddress;
26-
import java.net.SocketException;
2720
import java.nio.ByteBuffer;
2821

29-
import libcore.io.Libcore;
22+
import static android.os.Build.VERSION.SDK_INT;
3023

3124
/**
3225
* Access to low-level system functionality. Most of these are system calls. Most users will want
3326
* to use higher-level APIs where available, but this class provides access to the underlying
3427
* primitives used to implement the higher-level APIs.
3528
*
36-
* <p>The corresponding constants can be found in {@link OsConstants}.
29+
* <p>The corresponding constants can be found in {@link OsConstantsCompat}.
3730
*/
38-
@SuppressWarnings("unused")
39-
@TargetApi(21)
40-
public final class Os {
41-
private Os() {}
31+
public final class OsCompat {
32+
private OsCompat() {
33+
}
34+
35+
private static final Os IMPL;
36+
37+
static {
38+
if (SDK_INT >= 21) {
39+
IMPL = new OsApi21();
40+
} else {
41+
IMPL = new OsLibcore();
42+
}
43+
}
44+
45+
/**
46+
* See <a href="http://man7.org/linux/man-pages/man3/strerror.3.html">strerror(2)</a>.
47+
*/
48+
public static String strerror(int errno) {
49+
return IMPL.strerror(errno);
50+
}
4251

43-
/**
44-
* See <a href="http://man7.org/linux/man-pages/man2/write.2.html">write(2)</a>.
45-
*/
46-
public static int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
47-
try {
48-
return Libcore.os.write(fd, buffer);
49-
} catch (libcore.io.ErrnoException e) {
50-
throw new ErrnoException("write", e.errno);
52+
/**
53+
* See <a href="http://man7.org/linux/man-pages/man2/write.2.html">write(2)</a>.
54+
*/
55+
public static int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
56+
return IMPL.write(fd, buffer);
5157
}
52-
}
5358
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.system;
17+
18+
interface OsConstants {
19+
20+
String errnoName(int errno);
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package androidx.system;
2+
3+
import androidx.annotation.RequiresApi;
4+
5+
@RequiresApi(21)
6+
final class OsConstantsApi21 implements OsConstants {
7+
@Override
8+
public String errnoName(int errno) {
9+
return android.system.OsConstants.errnoName(errno);
10+
}
11+
}

server/src/main/java/android/system/OsConstants.java renamed to os-compat/src/main/java/androidx/system/OsConstantsCompat.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,31 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package android.system;
16+
package androidx.system;
1717

18-
import android.annotation.TargetApi;
18+
import android.system.Os;
1919

20-
@SuppressWarnings("unused")
21-
@TargetApi(21)
22-
public final class OsConstants {
20+
import static android.os.Build.VERSION.SDK_INT;
2321

24-
private OsConstants() { }
22+
public final class OsConstantsCompat {
23+
private OsConstantsCompat() {
24+
}
25+
26+
private static final OsConstants IMPL;
2527

26-
public static final int EINTR = libcore.io.OsConstants.EINTR;
28+
static {
29+
if (SDK_INT >= 21) {
30+
IMPL = new OsConstantsApi21();
31+
} else {
32+
IMPL = new OsConstantsLibcore();
33+
}
34+
}
2735

2836
/**
2937
* Returns the string name of an errno value.
3038
* For example, "EACCES". See {@link Os#strerror} for human-readable errno descriptions.
3139
*/
3240
public static String errnoName(int errno) {
33-
return libcore.io.OsConstants.errnoName(errno);
41+
return IMPL.errnoName(errno);
3442
}
3543
}

0 commit comments

Comments
 (0)