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
137 }
138
139 /*
140 * Converts Java time to DOS time.
141 */
142 public static long javaToDosTime(long time) {
143 Instant instant = Instant.ofEpochMilli(time);
144 LocalDateTime ldt = LocalDateTime.ofInstant(
145 instant, ZoneId.systemDefault());
146 int year = ldt.getYear() - 1980;
147 if (year < 0) {
148 return (1 << 21) | (1 << 16);
149 }
150 return (year << 25 |
151 ldt.getMonthValue() << 21 |
152 ldt.getDayOfMonth() << 16 |
153 ldt.getHour() << 11 |
154 ldt.getMinute() << 5 |
155 ldt.getSecond() >> 1) & 0xffffffffL;
156 }
157
158
159 // used to adjust values between Windows and java epoch
160 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
161 public static final long winToJavaTime(long wtime) {
162 return TimeUnit.MILLISECONDS.convert(
163 wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS, TimeUnit.MICROSECONDS);
164 }
165
166 public static final long javaToWinTime(long time) {
167 return (TimeUnit.MICROSECONDS.convert(time, TimeUnit.MILLISECONDS)
168 - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
169 }
170
171 public static final long unixToJavaTime(long utime) {
172 return TimeUnit.MILLISECONDS.convert(utime, TimeUnit.SECONDS);
173 }
174
175 public static final long javaToUnixTime(long time) {
176 return TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS);
177 }
|
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.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.concurrent.TimeUnit;
37 import java.util.regex.PatternSyntaxException;
38
39 /**
40 * @author Xueming Shen
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.
54 */
55 public static void writeInt(OutputStream os, long v) throws IOException {
56 os.write((int)(v & 0xff));
57 os.write((int)((v >>> 8) & 0xff));
58 os.write((int)((v >>> 16) & 0xff));
59 os.write((int)((v >>> 24) & 0xff));
60 }
61
135 }
136
137 /*
138 * Converts Java time to DOS time.
139 */
140 public static long javaToDosTime(long time) {
141 Instant instant = Instant.ofEpochMilli(time);
142 LocalDateTime ldt = LocalDateTime.ofInstant(
143 instant, ZoneId.systemDefault());
144 int year = ldt.getYear() - 1980;
145 if (year < 0) {
146 return (1 << 21) | (1 << 16);
147 }
148 return (year << 25 |
149 ldt.getMonthValue() << 21 |
150 ldt.getDayOfMonth() << 16 |
151 ldt.getHour() << 11 |
152 ldt.getMinute() << 5 |
153 ldt.getSecond() >> 1) & 0xffffffffL;
154 }
155
156 // used to adjust values between Windows and java epoch
157 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
158 public static final long winToJavaTime(long wtime) {
159 return TimeUnit.MILLISECONDS.convert(
160 wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS, TimeUnit.MICROSECONDS);
161 }
162
163 public static final long javaToWinTime(long time) {
164 return (TimeUnit.MICROSECONDS.convert(time, TimeUnit.MILLISECONDS)
165 - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
166 }
167
168 public static final long unixToJavaTime(long utime) {
169 return TimeUnit.MILLISECONDS.convert(utime, TimeUnit.SECONDS);
170 }
171
172 public static final long javaToUnixTime(long time) {
173 return TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS);
174 }
|