Skip to content

Commit

Permalink
8255078: sun/net/ftp/imp/FtpClient$MLSxParser uses wrong datetime format
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatev committed Oct 20, 2020
1 parent acacae5 commit a286753
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions 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
Expand Down Expand Up @@ -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(';');
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"));
}
}
Expand All @@ -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
Expand Down

0 comments on commit a286753

Please sign in to comment.