ParcelFileDescriptor使用说明

文件描述符,是一种程序读写已打开文件、socket 的对象。

FileDescriptor 对象,它代表了原始的 Linux 文件描述符
ParcelFileDescriptor 对象,是原始文件描述符的一个复制,对象跟 fd 不同,但都是操作同一个底层文件流以及文件位置指针

简单的 ParcelFileDescriptor 使用——pipe

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
public class DemoParcefliledescriptor extends AppCompatActivity {

private static final String TAG = "DemoPFD";

private static final String[] PERMISSIONS = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
private static final int PERMISSIONS_CODE = 3006;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_null);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(PERMISSIONS, PERMISSIONS_CODE);
}

FileOutputStream outputStream = new FileOutputStream(getStreamFd());
try {
outputStream.write(99);
outputStream.write(98);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}


private FileDescriptor getStreamFd() {
ParcelFileDescriptor[] pipes = null;

try {
pipes = ParcelFileDescriptor.createPipe();
new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(pipes[0])).start();
} catch (IOException e) {
e.printStackTrace();
}

return pipes[1].getFileDescriptor();
}

static class TransferThread extends Thread {
InputStream in;
FileOutputStream out;

TransferThread(InputStream in, FileOutputStream out) {
this.in = in;
this.out = out;
}

TransferThread(InputStream in) {
this.in = in;

File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/zlq_pdf");
Log.i(TAG, "File: " + outFile.getAbsolutePath());

try {
out = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

@Override
public void run() {
byte[] buf = new byte[1024*2];

int len;
try {
while((len=in.read(buf)) > 0) {
out.write(buf, 0, len);
Log.i(TAG, "out:" + len);

}

in.close();
out.flush();
out.getFD().sync();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

openssl设置加密套件

一个加密套件指明了 SSL 握手阶段和通信阶段所应该采用的各种算法。这些算法包括:认证算法、密钥交换算法、对称算法和摘要算法等。
在握手初始化的时候,双方都会导入各自所认可的多种加密套件。在握手阶段,由服务端选择其中的一种加密套件。
OpenSSL 的 ciphers 命令可以列出所有的加密套件。openssl 的加密套件在 s3_lib.c 的
ssl3_ciphers 数组中定义。比如有:

1
2
3
4
5
6
7
8
9
10
11
12
13
/_ Cipher 05 _/
{
1,
SSL3_TXT_RSA_RC4_128_SHA,
SSL3_CK_RSA_RC4_128_SHA,
SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_SHA1|SSL_SSLV3,
SSL_NOT_EXP|SSL_MEDIUM,
0,
128,
128,
SSL_ALL_CIPHERS,
SSL_ALL_STRENGTHS,
}

其中 1 表示是合法的加密套件;
SSL3_TXT_RSA_RC4_128_SHA 为加密套件的名字,
SSL3_CK_RSA_RC4_128_SHA 为 加密套件 ID ,
SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_SHA1|SSL_SSLV3 表明了各种算法,
其中密钥交换采用 RSA 算法(SSL_kRSA),认证采用 RSA 算法(SSL_aRSA),对称加密算法采用 RC4 算法(SSL_RC4),摘要采用 SHA1,采用 SSL 协议第三版本,
SSL_NOT_EXP|SSL_MEDIUM 表明算法的强度。

在客户端和服务器端建立安全连接之前,双方都必须指定适合自己的加密套件。加密套件的选择可以通过组合的字符串来控制。
字符串的形式举例:ALL:!ADH:RC4+RSA:+SSLv2:@STRENGTH。
Openssl 定义了 4 中选择符号:“+”,“-”,“!”,“@”。其中,“+”表示取交集;“-”表示临时删除一个算法;“!”表示永久删除一个算法;“@“表示了排序方法。
多个描述之间可以用“:”、“,”、“ ”、“;”来分开。选择加密套件的时候按照从左到的
顺序构成双向链表,存放与内存中。

ALL:!ADH:RC4+RSA:+SSLv2:@STRENGTH 表示的意义是:首先选择所有的加密套件
(不包含 eNULL,即空对称加密算法),然后在得到的双向链表之中去掉身份验证采用 DH
的加密套件;加入包含 RC4 算法并将包含 RSA 的加密套件放在双向链表的尾部;再将支持
SSLV2 的加密套件放在尾部;最后得到的结果按照安全强度进行排序。
SSL 建立链接之前,客户端和服务器端用 openssl 函数来设置自己支持的加密套件。主要
的函数有:

int SSL_set_cipher_list(SSL *s,const char *str);
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str);

比如只设置一种加密套件:
int ret=SSL_set_cipher_list(ssl,”RC4-MD5”);
如果服务端只设置了一种加密套件,那么客户端要么接受要么返回错误。加密套件的选
择是由服务端做出的。

vm-display-setting

  1. 启动 vm,然后点击菜单“虚拟机”-》“设置”-》“显示”
  2. 如下图设置

ubuntu升级第三方库

问题:
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.20’ not found (required by layout)

解决方式:
测试依赖:
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX

升级方法:

1
2
3
4
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

ppa 速度慢解决
将/etc/apt/sources.list 中
http://ppa.launchpad.net替换为https://launchpad.proxy.ustclug.org

ubuntu增加交换区

Process to Increase Size of Swap Partition and use it for Hibernation
Creating the swap partition
Activating the swap partition
Making the new swap partition work for hibernate (optional)
Creating the swap partition
Boot to Ubuntu install CD and choose the option to run Ubuntu now
Go to system -> GParted Partition Editor

Delete the swap partition and, if there is nothing else in it, the extended partition that holds it. (If by some miracle you’re able to resize your swap partition from here, I imagine your life will be a lot easier than mine.)
Decrease the size of your primary partition by the amount you want your new swap to be (I made mine 2x RAM + 500MB just to be safe). The easiest way to do this is to fill in the amount of space you want swap to be in the “free space following” field
In the free space that has now been created, choose new, type linux-swap and you can name the partition “swap” if you like
Hit the Apply button (should be a check mark) to write the changes to disk
When done, reboot back into Ubuntu
Activating the swap partition
(If your swap is on your primary hard drive, you don’t need to do anything here.) Now you need to find what partition your swap is on and what its UUID is. UUID?! you say? Well that’s the Universally Unique IDentifier for the partition so you can reference it even if it’s on a different mount point from boot-to-boot due to adding disks, etc.

Pull up a terminal and run gksu gparted & and enter your root password. The & lets this process run while still giving you access to the command line.

Right-click on your swap partition and choose Information. You should see the Path and UUID listed there. Keep this open for further reference.
Run gksu gedit /etc/fstab & and look for the line that has swap in it. It should be the third column, separated by spaces or tabs. You can either use the path or the UUID to tell Linux where to find your swap partition. I recommend UUID because it’ll stay constant even if you move the partition around or the disk somehow becomes sdb instead of sda or something like that. Make the appropriate edits and save the file. Your line should look something like this if you used UUID (with your UUID instead, of course):

UUID=41e86209-3802-424b-9a9d-d7683142dab7 none swap sw 0 0

or this if you used path: /dev/sda2 none swap sw 0 0

Save the file.
Enable the new swap partition with this command.

sudo swapon –all
OR

$ sudo swapon –all –verbose
swapon on /dev/sda2
swapon: /dev/sda2: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sda2: pagesize=4096, swapsize=2147483648, devsize=2147483648
Confirm that the swap partition exists.

$ cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 2097148 0 -1
Reboot to make sure the new swap gets activated properly at startup

ubuntu中java虚拟机内存分配不足

问题:

ubuntu 虚拟机中,编译 android10,出现如下错误:
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000769400000, 295698432, 0) failed; error=’Not enough space’ (errno=12)

解决方式:

方法一

最后来根据 (errno=12)这个搜索到一篇文章,说道:
    后来看了美军一个文章(http://www.enchantedage.com/node/235),加一个配置即可:echo 1000000 > /proc/sys/vm/max_map_count

我看了自己虚拟机 ubuntu 里面,/proc/sys/vm/max_map_count 的初始值很小,只有 65530,果断使用这个重设了一下。
  啊,终于好了,太艰难了。

后来又请装双系统的同事看下他们系统里的这个值,也是 65530,但是他们的却不报错,真是醉醉的。看来造成“无法分配内存”的原因,并不是这里,但是可以通过修改这个来解决。

猜测这个原因:1、双系统和虚拟机不同 2、安装 jdk 方式的不同(之前的我,和现在的同事们,都是先下载好 jdk 再安装的;可是现在虚拟机我却使用命令安装,这样不需要配置环境变量)

只是猜测,暂时就不去验证它了,如果再有遇到的同学,解决不了的话,可以朝这个方向尝试下。

方法二:

https://www.cnblogs.com/schips/p/solve_a_problem_of_Not-enough-space_when_building_android.html

编译rk3399板子android10系统(ubuntu22.4)

  1. 前提条件
    编译主机内存至少 12G,交换内存 10G(swapfile),磁盘空间 150G

  2. 解压 android10
    cat rk_android_10_sdk.tar.gz* | tar xzvf

  3. 进入解压根目录
    cd rk_android_10_sdk

  4. 切换终端
    exec bash
    注意:zsh 会有问题

  5. 安装 openjdk8

  6. 安装 python2.7
    sudo apt install python2.7

  7. 安装基础库

1
2
3
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip libssl-dev gawk liblz4-tool

sudo apt-get install libncurses5 libncurses5:i386
  1. 执行 sh javaenv.sh

  2. 执行 source build/envsetup.sh

  3. 执行 lunch
    出现菜单中,选择 6 rk3399_Android10-userdebug

  4. 如果修改 framework,需要
    make api-stubs-docs-update-current-api

  5. 执行 ./build.sh -UKAu

常见问题

  1. GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference
1
2
3
4
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5
sudo apt-get upgrade libstdc++6

ubuntu关闭zsh

终端执行 exec bash