Enabled partial parsing of audio resources.

pull/167/head
luccioman 7 years ago
parent fda0189613
commit bcbd0ae1a4

@ -226,22 +226,29 @@ public class audioTagParser extends AbstractParser implements Parser {
final int timezoneOffset, final int timezoneOffset,
final InputStream source) final InputStream source)
throws Parser.Failure, InterruptedException { throws Parser.Failure, InterruptedException {
return parseWithLimits(location, mimeType, charset, scraper, timezoneOffset, source, Integer.MAX_VALUE, Long.MAX_VALUE);
}
@Override
public Document[] parseWithLimits(final DigestURL location, final String mimeType, final String charset, final VocabularyScraper scraper,
final int timezoneOffset, final InputStream source, final int maxLinks, final long maxBytes)
throws UnsupportedOperationException, Failure, InterruptedException {
String filename = location.getFileName(); String filename = location.getFileName();
final String fileext = MultiProtocolURL.getFileExtension(filename); final String fileext = MultiProtocolURL.getFileExtension(filename);
filename = filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename); filename = filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename);
Document[] docs;
File tempFile = null; File tempFile = null;
AudioFile f; AudioFile f;
try { try {
boolean partiallyParsed = false;
if (location.isFile()) { if (location.isFile()) {
f = AudioFileIO.read(location.getFSFile()); f = AudioFileIO.read(location.getFSFile());
} else { } else {
// create a temporary file, as jaudiotagger requires a file rather than an input stream // create a temporary file, as jaudiotagger requires a file rather than an input stream
tempFile = File.createTempFile(filename, "." + fileext); tempFile = File.createTempFile(filename, "." + fileext);
FileUtils.copy(source, tempFile); long bytesCopied = FileUtils.copy(source, tempFile, maxBytes);
partiallyParsed = bytesCopied == maxBytes && source.read() != -1;
f = AudioFileIO.read(tempFile); f = AudioFileIO.read(tempFile);
} }
@ -292,7 +299,7 @@ public class audioTagParser extends AbstractParser implements Parser {
} }
} }
docs = new Document[]{new Document( final Document doc = new Document(
location, location,
mime, mime,
charset, charset,
@ -310,9 +317,9 @@ public class audioTagParser extends AbstractParser implements Parser {
null, null,
null, null,
false, false,
new Date()) new Date());
}; doc.setPartiallyParsed(partiallyParsed);
return docs; return new Document[]{doc};
} catch (final Exception e) { } catch (final Exception e) {
throw new Parser.Failure("Unexpected error while parsing audio file. " + e.getMessage(), location); throw new Parser.Failure("Unexpected error while parsing audio file. " + e.getMessage(), location);
} finally { } finally {
@ -325,6 +332,11 @@ public class audioTagParser extends AbstractParser implements Parser {
} }
} }
} }
@Override
public boolean isParseWithLimitsSupported() {
return true;
}
/** /**
* Process a tag field : add its value to the descriptions and to text. All * Process a tag field : add its value to the descriptions and to text. All

@ -237,6 +237,7 @@ public final class FileUtils {
* @param source InputStream instance * @param source InputStream instance
* @param dest File instance * @param dest File instance
* @param count the amount of bytes to copy (-1 for all, else must be greater than zero) * @param count the amount of bytes to copy (-1 for all, else must be greater than zero)
* @return the number of bytes actually copied (may be lower than count)
* @throws IOException when a read/write error occurred * @throws IOException when a read/write error occurred
* @throws NullPointerException when a parameter is null * @throws NullPointerException when a parameter is null
* @see #copy(InputStream source, OutputStream dest) * @see #copy(InputStream source, OutputStream dest)
@ -244,7 +245,7 @@ public final class FileUtils {
* @see #copy(File source, OutputStream dest) * @see #copy(File source, OutputStream dest)
* @see #copy(File source, File dest) * @see #copy(File source, File dest)
*/ */
public static void copy(final InputStream source, final File dest, final long count) throws IOException { public static long copy(final InputStream source, final File dest, final long count) throws IOException {
final String path = dest.getParent(); final String path = dest.getParent();
if ( path != null && path.length() > 0 ) { if ( path != null && path.length() > 0 ) {
new File(path).mkdirs(); new File(path).mkdirs();
@ -252,7 +253,7 @@ public final class FileUtils {
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
fos = new FileOutputStream(dest); fos = new FileOutputStream(dest);
copy(source, fos, count); return copy(source, fos, count);
} finally { } finally {
if ( fos != null ) { if ( fos != null ) {
try { try {

Loading…
Cancel
Save