파일을 base64로 인코딩하기 위해이 코드를 발견했습니다. 오프셋을 사용하고 있습니다. 이 코드에 대해 이해하지 못하는 어떤읽기 호출에서 오프셋 사용에 관한 설명
File filePath = new File("/sdcard/videooutput.mp4");
try{
FileInputStream fin=new FileInputStream(filePath);
long length = filePath.length();
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset<bytes.length && (numRead=fin.read(bytes, offset, bytes.length-offset))>=0){
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+filePath.getName());
}
Base64.encodeToString(bytes, Base64.DEFAULT);
는 while (offset < bytes.length && (numRead=fin.read(bytes, offset, bytes.length-offset)) >= 0)
입니다.
누구든지 코드가 수행하려고하는 것을 설명 할 수 있습니까? 나는 오프셋이 바이트의 길이보다 적은지 그리고 나머지는 확실하지 않은지만을 확인한다.
내 다음 질문은 오프셋을 사용하여 파일을 인코딩하는 이유는 무엇입니까?
이 질문에 대한 답변은 정말 도움이 될 것입니다. 이 유형의 질문에 대해 유감스럽게 생각하지만이 코드를 이해해야합니다.
오프셋은 "파일을 인코딩하는 데 사용되지 않습니다", 파일을 읽는 곳의 카운터입니다. 컴퓨터 재생 : 코드가 코드에서 실행될 때 변수 값을 적어 두십시오. 제공되는 코드 스 니펫에는 인코딩이 없습니다. –
'offset'을 사용하는 대신'DataInputStream'을 사용할 수 있습니다 –
그건 base64로 인코딩하지 않습니다. 그렇게하고 싶다면 핵심'javax.xml.bind.DataConverter' 또는 Apache Commons Codec을 사용할 수 있습니다. – LanguagesNamedAfterCofee