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.util.Arrays;
31 import java.util.Date;
32 import java.util.regex.PatternSyntaxException;
33 import java.util.concurrent.TimeUnit;
34
35 /**
36 *
37 * @author Xueming Shen
38 */
39
40 class ZipUtils {
41
42 /*
43 * Writes a 16-bit short to the output stream in little-endian byte order.
44 */
45 public static void writeShort(OutputStream os, int v) throws IOException {
46 os.write(v & 0xff);
47 os.write((v >>> 8) & 0xff);
48 }
49
50 /*
51 * Writes a 32-bit int to the output stream in little-endian byte order.
86 public static void writeBytes(OutputStream os, byte[] b, int off, int len)
87 throws IOException
88 {
89 os.write(b, off, len);
90 }
91
92 /*
93 * Append a slash at the end, if it does not have one yet
94 */
95 public static byte[] toDirectoryPath(byte[] dir) {
96 if (dir.length != 0 && dir[dir.length - 1] != '/') {
97 dir = Arrays.copyOf(dir, dir.length + 1);
98 dir[dir.length - 1] = '/';
99 }
100 return dir;
101 }
102
103 /*
104 * Converts DOS time to Java time (number of milliseconds since epoch).
105 */
106 @SuppressWarnings("deprecation")
107 public static long dosToJavaTime(long dtime) {
108 Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
109 (int)(((dtime >> 21) & 0x0f) - 1),
110 (int)((dtime >> 16) & 0x1f),
111 (int)((dtime >> 11) & 0x1f),
112 (int)((dtime >> 5) & 0x3f),
113 (int)((dtime << 1) & 0x3e));
114 return d.getTime();
115 }
116
117 /*
118 * Converts Java time to DOS time.
119 */
120 @SuppressWarnings("deprecation")
121 public static long javaToDosTime(long time) {
122 Date d = new Date(time);
123 int year = d.getYear() + 1900;
124 if (year < 1980) {
125 return (1 << 21) | (1 << 16);
126 }
127 return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
128 d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
129 d.getSeconds() >> 1;
130 }
131
132
133 // used to adjust values between Windows and java epoch
134 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
135 public static final long winToJavaTime(long wtime) {
136 return TimeUnit.MILLISECONDS.convert(
137 wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS, TimeUnit.MICROSECONDS);
138 }
139
140 public static final long javaToWinTime(long time) {
141 return (TimeUnit.MICROSECONDS.convert(time, TimeUnit.MILLISECONDS)
142 - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
143 }
144
145 public static final long unixToJavaTime(long utime) {
146 return TimeUnit.MILLISECONDS.convert(utime, TimeUnit.SECONDS);
147 }
148
149 public static final long javaToUnixTime(long time) {
|
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.Instant;
31 import java.time.LocalDateTime;
32 import java.time.ZoneId;
33 import java.util.Arrays;
34 import java.util.regex.PatternSyntaxException;
35 import java.util.concurrent.TimeUnit;
36
37 /**
38 *
39 * @author Xueming Shen
40 */
41
42 class ZipUtils {
43
44 /*
45 * Writes a 16-bit short to the output stream in little-endian byte order.
46 */
47 public static void writeShort(OutputStream os, int v) throws IOException {
48 os.write(v & 0xff);
49 os.write((v >>> 8) & 0xff);
50 }
51
52 /*
53 * Writes a 32-bit int to the output stream in little-endian byte order.
88 public static void writeBytes(OutputStream os, byte[] b, int off, int len)
89 throws IOException
90 {
91 os.write(b, off, len);
92 }
93
94 /*
95 * Append a slash at the end, if it does not have one yet
96 */
97 public static byte[] toDirectoryPath(byte[] dir) {
98 if (dir.length != 0 && dir[dir.length - 1] != '/') {
99 dir = Arrays.copyOf(dir, dir.length + 1);
100 dir[dir.length - 1] = '/';
101 }
102 return dir;
103 }
104
105 /*
106 * Converts DOS time to Java time (number of milliseconds since epoch).
107 */
108 public static long dosToJavaTime(long dtime) {
109 LocalDateTime ldt = LocalDateTime.of(
110 (int) (((dtime >> 25) & 0x7f) + 1980),
111 (int) ((dtime >> 21) & 0x0f),
112 (int) ((dtime >> 16) & 0x1f),
113 (int) ((dtime >> 11) & 0x1f),
114 (int) ((dtime >> 5) & 0x3f),
115 (int) ((dtime << 1) & 0x3e));
116 return ldt.toEpochSecond(
117 ZoneId.systemDefault().getRules().getOffset(ldt)) * 1000L;
118 }
119
120 /*
121 * Converts Java time to DOS time.
122 */
123 public static long javaToDosTime(long time) {
124 Instant instant = Instant.ofEpochMilli(time);
125 LocalDateTime ldt = LocalDateTime.ofInstant(
126 instant, ZoneId.systemDefault());
127 int year = ldt.getYear() - 1980;
128 if (year < 0) {
129 return (1 << 21) | (1 << 16);
130 }
131 return (year << 25 |
132 ldt.getMonthValue() << 21 |
133 ldt.getDayOfMonth() << 16 |
134 ldt.getHour() << 11 |
135 ldt.getMinute() << 5 |
136 ldt.getSecond() >> 1) & 0xffffffffL;
137 }
138
139
140 // used to adjust values between Windows and java epoch
141 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
142 public static final long winToJavaTime(long wtime) {
143 return TimeUnit.MILLISECONDS.convert(
144 wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS, TimeUnit.MICROSECONDS);
145 }
146
147 public static final long javaToWinTime(long time) {
148 return (TimeUnit.MICROSECONDS.convert(time, TimeUnit.MILLISECONDS)
149 - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
150 }
151
152 public static final long unixToJavaTime(long utime) {
153 return TimeUnit.MILLISECONDS.convert(utime, TimeUnit.SECONDS);
154 }
155
156 public static final long javaToUnixTime(long time) {
|