Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructors for FileDescriptors #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions src/main/java/com/mpatric/mp3agic/FileWrapper.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,87 @@
package com.mpatric.mp3agic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.TimeUnit;

public class FileWrapper {

protected Path path;
protected long length;
protected long lastModified;
private interface SeekableByteChannelFactory {
SeekableByteChannel createByteChannel() throws IOException;
}

private SeekableByteChannelFactory byteChannelFactory;
private String filename;
private Path absolutePath;
private long length;
private long lastModified;

protected FileWrapper() {
}

public FileWrapper(FileDescriptor fileDescriptor) throws IOException {
if (fileDescriptor == null) throw new NullPointerException();
if (!fileDescriptor.valid()) throw new IOException("FileDescriptor not valid");
length = -1;
lastModified = -1;
byteChannelFactory = () -> {
FileChannel channel = new FileInputStream(fileDescriptor).getChannel();
length = channel.size();
return channel;
};
}

public FileWrapper(String filename) throws IOException {
this.path = Paths.get(filename);
init();
this(Paths.get(filename));
}

public FileWrapper(File file) throws IOException {
if (file == null) throw new NullPointerException();
this.path = Paths.get(file.getPath());
init();
this(Paths.get(file.getPath()));
}

public FileWrapper(Path path) throws IOException {
if (path == null) throw new NullPointerException();
this.path = path;
init();
init(path);
}

private void init() throws IOException {
private void init(Path path) throws IOException {
if (!Files.exists(path)) throw new FileNotFoundException("File not found " + path);
if (!Files.isReadable(path)) throw new IOException("File not readable");
absolutePath = path.toAbsolutePath();
byteChannelFactory = () -> Files.newByteChannel(path, StandardOpenOption.READ);
filename = path.toString();
length = Files.size(path);
lastModified = Files.getLastModifiedTime(path).to(TimeUnit.MILLISECONDS);
}

public String getFilename() {
return path.toString();
if (filename == null) {
throw new IllegalArgumentException("Cannot get filename from a FileDescriptor");
}
return filename;
}

public long getLength() {
return length;
}

public long getLastModified() {
if (lastModified < 0) {
throw new IllegalArgumentException("Cannot get last modified from a FileDescriptor");
}
return lastModified;
}

protected SeekableByteChannel getByteChannel() throws IOException {
return byteChannelFactory.createByteChannel();
}

protected Path getAbsolutePath() {
return absolutePath;
}
}
24 changes: 21 additions & 3 deletions src/main/java/com/mpatric/mp3agic/Mp3File.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.mpatric.mp3agic;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.EnumSet;
import java.util.Objects;

public class Mp3File extends FileWrapper {

Expand Down Expand Up @@ -71,6 +73,19 @@ public Mp3File(File file, int bufferLength, boolean scanFile) throws IOException
init(bufferLength, scanFile);
}

public Mp3File(FileDescriptor fileDescriptor) throws IOException, UnsupportedTagException, InvalidDataException {
this(fileDescriptor, DEFAULT_BUFFER_LENGTH, true);
}

public Mp3File(FileDescriptor fileDescriptor, int bufferLength) throws IOException, UnsupportedTagException, InvalidDataException {
this(fileDescriptor, bufferLength, true);
}

public Mp3File(FileDescriptor fileDescriptor, int bufferLength, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
super(fileDescriptor);
init(bufferLength, scanFile);
}

public Mp3File(Path path) throws IOException, UnsupportedTagException, InvalidDataException {
this(path, DEFAULT_BUFFER_LENGTH, true);
}
Expand All @@ -90,7 +105,7 @@ private void init(int bufferLength, boolean scanFile) throws IOException, Unsupp
this.bufferLength = bufferLength;
this.scanFile = scanFile;

try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ)) {
try (SeekableByteChannel seekableByteChannel = getByteChannel()) {
initId3v1Tag(seekableByteChannel);
scanFile(seekableByteChannel);
if (startOffset < 0) {
Expand Down Expand Up @@ -440,7 +455,10 @@ public void removeCustomTag() {
}

public void save(String newFilename) throws IOException, NotSupportedException {
if (path.toAbsolutePath().compareTo(Paths.get(newFilename).toAbsolutePath()) == 0) {
if (getAbsolutePath() == null) {
throw new IllegalArgumentException("Unable to save files based on FileDescriptors");
}
if (Objects.equals(getAbsolutePath(), Paths.get(newFilename).toAbsolutePath())) {
throw new IllegalArgumentException("Save filename same as source filename");
}
try (SeekableByteChannel saveFile = Files.newByteChannel(Paths.get(newFilename), EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))) {
Expand Down Expand Up @@ -470,7 +488,7 @@ private void saveMpegFrames(SeekableByteChannel saveFile) throws IOException {
if (filePos < 0) return;
if (endOffset < filePos) return;
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength);
try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ)) {
try (SeekableByteChannel seekableByteChannel = getByteChannel()) {
seekableByteChannel.position(filePos);
while (true) {
byteBuffer.clear();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/mpatric/mp3agic/FileWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

import static com.mpatric.mp3agic.TestHelper.toFileDescriptor;
import static org.junit.Assert.*;

public class FileWrapperTest {
Expand All @@ -17,6 +19,15 @@ public class FileWrapperTest {
private static final String NON_EXISTENT_FILENAME = "just-not.there";
private static final String MALFORMED_FILENAME = "malformed.\0";

@Test
public void shouldReadValidFileDescriptor() throws IOException {
FileWrapper fileWrapper = new FileWrapper(toFileDescriptor(new File(VALID_FILENAME)));
assertEquals(fileWrapper.getLength(), -1); // length not valid until channel is opened
try (SeekableByteChannel channel = fileWrapper.getByteChannel()) {
assertEquals(fileWrapper.getLength(), VALID_FILE_LENGTH);
}
}

@Test
public void shouldReadValidFilename() throws IOException {
FileWrapper fileWrapper = new FileWrapper(VALID_FILENAME);
Expand Down Expand Up @@ -67,6 +78,11 @@ public void shouldFailForNullFilenameFile() throws IOException {
new FileWrapper((java.io.File) null);
}

@Test(expected = NullPointerException.class)
public void shouldFailForNullFileDescriptor() throws IOException {
new FileWrapper((java.io.FileDescriptor) null);
}

@Test(expected = NullPointerException.class)
public void shouldFailForNullPath() throws IOException {
new FileWrapper((java.nio.file.Path) null);
Expand Down
Loading