1 /*
2 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.time.DateTimeException;
31 import java.time.Instant;
32 import java.time.LocalDateTime;
33 import java.time.ZoneId;
34 import java.util.Arrays;
35 import java.util.Date;
36 import java.util.regex.PatternSyntaxException;
37 import java.util.concurrent.TimeUnit;
38
39 /**
40 *
41 * @author Xueming Shen
42 */
43
44 class ZipUtils {
45
46 /*
47 * Writes a 16-bit short to the output stream in little-endian byte order.
48 */
49 public static void writeShort(OutputStream os, int v) throws IOException {
50 os.write(v & 0xff);
51 os.write((v >>> 8) & 0xff);
52 }
53
54 /*
55 * Writes a 32-bit int to the output stream in little-endian byte order.
56 */
57 public static void writeInt(OutputStream os, long v) throws IOException {
58 os.write((int)(v & 0xff));
59 os.write((int)((v >>> 8) & 0xff));
60 os.write((int)((v >>> 16) & 0xff));
61 os.write((int)((v >>> 24) & 0xff));
62 }
63
64 /*
|
1 /*
2 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.nio.file.attribute.PosixFilePermission;
31 import java.time.DateTimeException;
32 import java.time.Instant;
33 import java.time.LocalDateTime;
34 import java.time.ZoneId;
35 import java.util.Arrays;
36 import java.util.Date;
37 import java.util.HashSet;
38 import java.util.Set;
39 import java.util.concurrent.TimeUnit;
40 import java.util.regex.PatternSyntaxException;
41
42 /**
43 * @author Xueming Shen
44 */
45 class ZipUtils {
46
47 /**
48 * The value indicating unix file attributes in CEN field "version made by".
49 */
50 static final int FILE_ATTRIBUTES_UNIX = 3;
51
52 /**
53 * Constant used to calculate "version made by".
54 */
55 static final int VERSION_BASE_UNIX = FILE_ATTRIBUTES_UNIX << 8;
56
57 /**
58 * The bit flag used to specify read permission by the owner.
59 */
60 static final int POSIX_USER_READ = 0400;
61
62 /**
63 * The bit flag used to specify write permission by the owner.
64 */
65 static final int POSIX_USER_WRITE = 0200;
66
67 /**
68 * The bit flag used to specify execute permission by the owner.
69 */
70 static final int POSIX_USER_EXECUTE = 0100;
71
72 /**
73 * The bit flag used to specify read permission by the group.
74 */
75 static final int POSIX_GROUP_READ = 040;
76
77 /**
78 * The bit flag used to specify write permission by the group.
79 */
80 static final int POSIX_GROUP_WRITE = 020;
81
82 /**
83 * The bit flag used to specify execute permission by the group.
84 */
85 static final int POSIX_GROUP_EXECUTE = 010;
86
87 /**
88 * The bit flag used to specify read permission by others.
89 */
90 static final int POSIX_OTHER_READ = 04;
91
92 /**
93 * The bit flag used to specify write permission by others.
94 */
95 static final int POSIX_OTHER_WRITE = 02;
96
97 /**
98 * The bit flag used to specify execute permission by others.
99 */
100 static final int POSIX_OTHER_EXECUTE = 01;
101
102 /**
103 * Convert a {@link PosixFilePermission} object into the appropriate bit
104 * flag.
105 *
106 * @param perm The {@link PosixFilePermission} object.
107 * @return The bit flag as int.
108 */
109 private static int permToFlag(PosixFilePermission perm) {
110 switch(perm) {
111 case OWNER_READ:
112 return POSIX_USER_READ;
113 case OWNER_WRITE:
114 return POSIX_USER_WRITE;
115 case OWNER_EXECUTE:
116 return POSIX_USER_EXECUTE;
117 case GROUP_READ:
118 return POSIX_GROUP_READ;
119 case GROUP_WRITE:
120 return POSIX_GROUP_WRITE;
121 case GROUP_EXECUTE:
122 return POSIX_GROUP_EXECUTE;
123 case OTHERS_READ:
124 return POSIX_OTHER_READ;
125 case OTHERS_WRITE:
126 return POSIX_OTHER_WRITE;
127 case OTHERS_EXECUTE:
128 return POSIX_OTHER_EXECUTE;
129 default:
130 return 0;
131 }
132 }
133
134 /**
135 * Converts a set of {@link PosixFilePermission}s into an int value where
136 * the according bits are set.
137 *
138 * @param perms A Set of {@link PosixFilePermission} objects.
139 *
140 * @return A bit mask representing the input Set.
141 */
142 static int permsToFlags(Set<PosixFilePermission> perms) {
143 if (perms == null) {
144 return -1;
145 }
146 int flags = 0;
147 for (PosixFilePermission perm : perms) {
148 flags |= permToFlag(perm);
149 }
150 return flags;
151 }
152
153 /**
154 * Converts a bit mask of Posix file permissions into a mutable
155 * set of {@link PosixFilePermission} objects.
156 *
157 * @param flags The bit mask containing the flags.
158 *
159 * @return A set of {@link PosixFilePermission} objects matching the input
160 * flags.
161 */
162 static Set<PosixFilePermission> permsFromFlags(int flags) {
163 Set<PosixFilePermission> perms = new HashSet<>(PosixFilePermission.values().length);
164 for (PosixFilePermission perm : PosixFilePermission.values()) {
165 if ((flags & permToFlag(perm)) != 0) {
166 perms.add(perm);
167 }
168 }
169 return perms;
170 }
171
172 /*
173 * Writes a 16-bit short to the output stream in little-endian byte order.
174 */
175 public static void writeShort(OutputStream os, int v) throws IOException {
176 os.write(v & 0xff);
177 os.write((v >>> 8) & 0xff);
178 }
179
180 /*
181 * Writes a 32-bit int to the output stream in little-endian byte order.
182 */
183 public static void writeInt(OutputStream os, long v) throws IOException {
184 os.write((int)(v & 0xff));
185 os.write((int)((v >>> 8) & 0xff));
186 os.write((int)((v >>> 16) & 0xff));
187 os.write((int)((v >>> 24) & 0xff));
188 }
189
190 /*
|