mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8057113: (fs) Path should have a method to obtain the filename extension
Reviewed-by: rriggs, lancea, mr, alanb
This commit is contained in:
parent
d17bf51f91
commit
50d91a31d4
2 changed files with 145 additions and 2 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -31,6 +31,7 @@ import java.net.URI;
|
||||||
import java.nio.file.spi.FileSystemProvider;
|
import java.nio.file.spi.FileSystemProvider;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object that may be used to locate a file in a file system. It will
|
* An object that may be used to locate a file in a file system. It will
|
||||||
|
@ -49,7 +50,7 @@ import java.util.NoSuchElementException;
|
||||||
* file system. {@code Path} defines the {@link #getFileName() getFileName},
|
* file system. {@code Path} defines the {@link #getFileName() getFileName},
|
||||||
* {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath
|
* {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath
|
||||||
* subpath} methods to access the path components or a subsequence of its name
|
* subpath} methods to access the path components or a subsequence of its name
|
||||||
* elements.
|
* elements, and {@link #getExtension() getExtension} to obtain its extension.
|
||||||
*
|
*
|
||||||
* <p> In addition to accessing the components of a path, a {@code Path} also
|
* <p> In addition to accessing the components of a path, a {@code Path} also
|
||||||
* defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path)
|
* defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path)
|
||||||
|
@ -248,6 +249,63 @@ public interface Path
|
||||||
*/
|
*/
|
||||||
Path getFileName();
|
Path getFileName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the file extension of this path's file name as a {@code String}.
|
||||||
|
* The extension is derived from this {@code Path} by obtaining the
|
||||||
|
* {@linkplain #getFileName file name element}, deriving its {@linkplain
|
||||||
|
* #toString string representation}, and then extracting a substring
|
||||||
|
* determined by the position of a period character ('.', U+002E FULL STOP)
|
||||||
|
* within the file name string. If the file name element is {@code null},
|
||||||
|
* or if the file name string does not contain a period character, or if
|
||||||
|
* the only period in the file name string is its first character, then
|
||||||
|
* the extension is {@code null}. Otherwise, the extension is the substring
|
||||||
|
* after the last period in the file name string. If this last period is
|
||||||
|
* also the last character in the file name string, then the extension is
|
||||||
|
* {@linkplain String#isEmpty empty}.
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* The default implementation is equivalent for this path to:
|
||||||
|
* <pre>{@code
|
||||||
|
* int lastPeriod = fileName.lastIndexOf('.');
|
||||||
|
* if (lastPeriod <= 0)
|
||||||
|
* return null;
|
||||||
|
* return (lastPeriod == fileName.length() - 1)
|
||||||
|
* ? ""
|
||||||
|
* : fileName.substring(lastPeriod + 1);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @return the file name extension of this path, which might be the
|
||||||
|
* empty string, or {@code null} if no extension is found
|
||||||
|
*
|
||||||
|
* @since 20
|
||||||
|
*/
|
||||||
|
default String getExtension() {
|
||||||
|
Path fileName = getFileName();
|
||||||
|
if (fileName == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
String fileNameString = fileName.toString();
|
||||||
|
int length = fileNameString.length();
|
||||||
|
|
||||||
|
// An empty or unity length file name string has a null extension
|
||||||
|
if (length > 1) {
|
||||||
|
int lastPeriodIndex = fileNameString.lastIndexOf('.');
|
||||||
|
|
||||||
|
// Indeterminate if there is no period character or
|
||||||
|
// only the first character is a period character
|
||||||
|
if (lastPeriodIndex > 0) {
|
||||||
|
if (lastPeriodIndex == length - 1) {
|
||||||
|
// empty string
|
||||||
|
return "";
|
||||||
|
} else {
|
||||||
|
return fileNameString.substring(lastPeriodIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the <em>parent path</em>, or {@code null} if this path does not
|
* Returns the <em>parent path</em>, or {@code null} if this path does not
|
||||||
* have a parent.
|
* have a parent.
|
||||||
|
|
85
test/jdk/java/nio/file/Path/Extensions.java
Normal file
85
test/jdk/java/nio/file/Path/Extensions.java
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, 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
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.DataProvider;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @bug 8057113
|
||||||
|
* @summary Verify getExtension method
|
||||||
|
* @run testng Extensions
|
||||||
|
*/
|
||||||
|
public class Extensions {
|
||||||
|
/**
|
||||||
|
* Returns path name string and expected extension pairs.
|
||||||
|
*
|
||||||
|
* @return {@code {{"pathname", "extension"},...}}
|
||||||
|
*/
|
||||||
|
@DataProvider
|
||||||
|
static Object[][] getProvider() {
|
||||||
|
Object[][] pairs = {
|
||||||
|
{"", null},
|
||||||
|
{".", null},
|
||||||
|
{"..", ""},
|
||||||
|
{"...", ""},
|
||||||
|
{"....", ""},
|
||||||
|
{".....", ""},
|
||||||
|
{"aa", null},
|
||||||
|
{"a.", ""},
|
||||||
|
{".a", null},
|
||||||
|
{"..a", "a"},
|
||||||
|
{"...a", "a"},
|
||||||
|
{"....a", "a"},
|
||||||
|
{".a.b", "b"},
|
||||||
|
{"...a.b", "b"},
|
||||||
|
{"...a.b.", ""},
|
||||||
|
{"..foo", "foo"},
|
||||||
|
{"foo.", ""},
|
||||||
|
{"test.", ""},
|
||||||
|
{"test..", ""},
|
||||||
|
{"test...", ""},
|
||||||
|
{"test.rb", "rb"},
|
||||||
|
{"a/b/d/test.rb" , "rb"},
|
||||||
|
{".a/b/d/test.rb", "rb"},
|
||||||
|
{"test", null},
|
||||||
|
{".profile", null},
|
||||||
|
{".profile.sh", "sh"},
|
||||||
|
{"foo.tar.gz", "gz"},
|
||||||
|
{"foo.bar.", ""},
|
||||||
|
{"archive.zip", "zip"},
|
||||||
|
{"compress.gzip", "gzip"},
|
||||||
|
{"waitwhat.&$!#%", "&$!#%"},
|
||||||
|
{"6.283185307", "283185307"}
|
||||||
|
};
|
||||||
|
return pairs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider = "getProvider")
|
||||||
|
public static void get(String pathname, String extension) {
|
||||||
|
Assert.assertEquals(Path.of(pathname).getExtension(), extension);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue