forked from youngmonkeys/ezyhttp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update ResourceRequestHandler (youngmonkeys#44)
- Loading branch information
Showing
16 changed files
with
989 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/data/BytesRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.tvd12.ezyhttp.core.data; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class BytesRange { | ||
private final long from; | ||
private final long to; | ||
|
||
public BytesRange(String range) { | ||
this(extractRange(range)); | ||
} | ||
|
||
public BytesRange(String[] fromTo) { | ||
this( | ||
Long.parseLong(fromTo[0].trim()), | ||
fromTo.length == 1 ? 0 : Long.parseLong(fromTo[1].trim()) | ||
); | ||
} | ||
|
||
private static String[] extractRange(String range) { | ||
final int equalsIndex = range.indexOf('='); | ||
String actualRange = range; | ||
if (equalsIndex >= 0) { | ||
actualRange = range | ||
.substring(equalsIndex + 1) | ||
.trim(); | ||
} | ||
final int dashIndex = actualRange.lastIndexOf('-'); | ||
if (dashIndex <= 0) { | ||
return new String[] { actualRange }; | ||
} | ||
if (dashIndex >= actualRange.length() - 1) { | ||
return new String[] { | ||
actualRange.substring(0, dashIndex) | ||
}; | ||
} | ||
return new String[] { | ||
actualRange.substring(0, dashIndex).trim(), | ||
actualRange.substring(dashIndex + 1).trim() | ||
}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/AnywayFileLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.tvd12.ezyhttp.core.io; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class AnywayFileLoader { | ||
|
||
private final List<Function<String, File>> loaders = | ||
Arrays.asList( | ||
File::new, | ||
filePath -> newFileFromUrl( | ||
getClass().getResource(filePath) | ||
), | ||
filePath -> newFileFromUrl( | ||
getClass().getClassLoader().getResource(filePath) | ||
), | ||
filePath -> newFileFromUrl( | ||
getClass().getResource('/' + filePath) | ||
), | ||
filePath -> newFileFromUrl( | ||
getClass().getClassLoader().getResource('/' + filePath) | ||
), | ||
filePath -> newFileFromUrl( | ||
Thread.currentThread().getContextClassLoader().getResource(filePath) | ||
), | ||
filePath -> newFileFromUrl( | ||
Thread.currentThread().getContextClassLoader().getResource('/' + filePath) | ||
) | ||
); | ||
|
||
public File load(String filePath) { | ||
for (Function<String, File> loader : loaders) { | ||
final File file = loader.apply(filePath); | ||
if (file != null && file.exists()) { | ||
return file; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static File newFileFromUrl(URL fileUrl) { | ||
return fileUrl != null ? new File(fileUrl.getFile()) : null; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/io/BytesRangeFileInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.tvd12.ezyhttp.core.io; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
|
||
import com.tvd12.ezyhttp.core.data.BytesRange; | ||
|
||
import lombok.Getter; | ||
|
||
public class BytesRangeFileInputStream extends InputStream { | ||
|
||
@Getter | ||
private final long from; | ||
@Getter | ||
private final long to; | ||
@Getter | ||
private final long fileLength; | ||
@Getter | ||
private long readBytes; | ||
@Getter | ||
private final long targetReadBytes; | ||
private final RandomAccessFile randomAccessFile; | ||
|
||
public static final int MAX_CHUNK_LENGTH = 2 * 1024 * 1024; | ||
|
||
public BytesRangeFileInputStream( | ||
String filePath, | ||
String range | ||
) throws Exception { | ||
this(filePath, new BytesRange(range)); | ||
} | ||
|
||
public BytesRangeFileInputStream( | ||
String filePath, | ||
BytesRange range | ||
) throws Exception { | ||
this( | ||
filePath, | ||
range.getFrom(), | ||
range.getTo() | ||
); | ||
} | ||
|
||
public BytesRangeFileInputStream( | ||
String filePath, | ||
long rangeFrom, | ||
long rangeTo | ||
) throws Exception { | ||
from = rangeFrom; | ||
final AnywayFileLoader fileLoader = new AnywayFileLoader(); | ||
final File file = fileLoader.load(filePath); | ||
if (file == null) { | ||
throw new FileNotFoundException(filePath + " not found"); | ||
} | ||
fileLength = file.length(); | ||
long actualTo = rangeTo == 0 | ||
? from + MAX_CHUNK_LENGTH | ||
: rangeTo + 1; | ||
if (actualTo > fileLength) { | ||
actualTo = fileLength; | ||
} | ||
to = actualTo; | ||
targetReadBytes = actualTo - from; | ||
randomAccessFile = new RandomAccessFile( | ||
file, | ||
"r" | ||
); | ||
try { | ||
randomAccessFile.seek(from); | ||
} catch (Exception e) { | ||
randomAccessFile.close(); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public int read(byte[] b) throws IOException { | ||
if (readBytes >= targetReadBytes) { | ||
return -1; | ||
} | ||
final int length = (int) (to - (from + readBytes)); | ||
final int actualLength = Math.min(b.length, length); | ||
final int rb = randomAccessFile.read(b, 0, actualLength); | ||
if (rb > 0) { | ||
readBytes += rb; | ||
} | ||
return rb; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (readBytes >= targetReadBytes) { | ||
return -1; | ||
} | ||
final int b = randomAccessFile.read(); | ||
if (b >= 0) { | ||
++readBytes; | ||
} | ||
return b; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
randomAccessFile.close(); | ||
} | ||
|
||
public String getBytesContentRangeString() { | ||
return "bytes " + from + | ||
'-' + (from < to ? to - 1 : to) + | ||
'/' + fileLength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.