From a286753942c3eaf1083bceba231fe9d953aef3ec Mon Sep 17 00:00:00 2001 From: iignatyev Date: Tue, 20 Oct 2020 15:38:34 -0700 Subject: [PATCH] 8255078: sun/net/ftp/imp/FtpClient$MLSxParser uses wrong datetime format --- .../classes/sun/net/ftp/impl/FtpClient.java | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java b/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java index 5cf7f30d7cf73..bb42cc1dff381 100644 --- a/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java +++ b/src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -289,9 +289,6 @@ public FtpDirEntry parseLine(String line) { } private class MLSxParser implements FtpDirParser { - - private SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss"); - public FtpDirEntry parseLine(String line) { String name = null; int i = line.lastIndexOf(';'); @@ -326,22 +323,14 @@ public FtpDirEntry parseLine(String line) { } s = file.getFact("Modify"); if (s != null) { - Date d = null; - try { - d = df.parse(s); - } catch (ParseException ex) { - } + Date d = parseRfc3659TimeValue(s); if (d != null) { file.setLastModified(d); } } s = file.getFact("Create"); if (s != null) { - Date d = null; - try { - d = df.parse(s); - } catch (ParseException ex) { - } + Date d = parseRfc3659TimeValue(s); if (d != null) { file.setCreated(d); } @@ -1749,15 +1738,17 @@ public long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOExce } return -1; } - private static String[] MDTMformats = { - "yyyyMMddHHmmss.SSS", - "yyyyMMddHHmmss" - }; - private static SimpleDateFormat[] dateFormats = new SimpleDateFormat[MDTMformats.length]; + + private static final SimpleDateFormat[] dateFormats; static { - for (int i = 0; i < MDTMformats.length; i++) { - dateFormats[i] = new SimpleDateFormat(MDTMformats[i]); + String[] formats = { + "yyyyMMddHHmmss.SSS", + "yyyyMMddHHmmss" + }; + dateFormats = new SimpleDateFormat[formats.length]; + for (int i = 0; i < formats.length; ++i) { + dateFormats[i] = new SimpleDateFormat(formats[i]); dateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT")); } } @@ -1778,20 +1769,25 @@ public Date getLastModified(String path) throws sun.net.ftp.FtpProtocolException issueCommandCheck("MDTM " + path); if (lastReplyCode == FtpReplyCode.FILE_STATUS) { String s = getResponseString().substring(4); - Date d = null; - for (SimpleDateFormat dateFormat : dateFormats) { - try { - d = dateFormat.parse(s); - } catch (ParseException ex) { - } - if (d != null) { - return d; - } - } + return parseRfc3659TimeValue(s); } return null; } + private static Date parseRfc3659TimeValue(String s) { + Date d = null; + for (SimpleDateFormat dateFormat : dateFormats) { + try { + d = dateFormat.parse(s); + } catch (ParseException ex) { + } + if (d != null) { + return d; + } + } + return d; + } + /** * Sets the parser used to handle the directory output to the specified * one. By default the parser is set to one that can handle most FTP