gpt4 book ai didi

java - 加载字节数组花费太长时间?

转载 作者:行者123 更新时间:2023-12-01 11:52:55 24 4
gpt4 key购买 nike

我雇了人来编写我的应用程序。它将从互联网下载的乐器加载到我的应用程序中。下载乐器的速度很快,但加载乐器时需要很长时间,至少一分钟或更长时间。我查看了代码,看看它会在哪里减慢速度,但我似乎无法弄清楚。任何帮助表示赞赏。

代码: //文件加载任务

 class SaveInputStreamTask extends AsyncTask<String, Integer, String> {

private Context context;
ProgressDialog mProgressDialog;

public SaveInputStreamTask(Context context) {
this.context = context;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();

// mProgressDialog = new ProgressDialog(context);
//mProgressDialog.setMessage("Beat Will Take A Minute To Load When Mixing So Start Recording");
mProgressDialog = ProgressDialog.show(context, getResources().getString(R.string.app_name), "Beat Will Take Up To A Minute To Load. In The Meantime How's Your Day?");
mProgressDialog.show();

}

@Override
protected String doInBackground(String... sUrl) {

try
{
File file = new File(instrument_file_name);
long totalFilesize = file.length();
long readSize = 0;
FileInputStream fis = new FileInputStream(file);
saveInputStream(fis);
return "SUCCESS";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}

@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}

@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
if(result == null){
Toast.makeText(context, "Loading Beat failed. Please try again", Toast.LENGTH_SHORT).show();
RecordRap.this.finish();
}
else
{

}
}

public void saveInputStream(InputStream is) throws IOException
{
int n = 0;
DataInputStream in1;
in1 = new DataInputStream(is);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

try
{

while ((n = in1.read()) != -1)
{
bos.write(n);
}
}
catch (IOException e)
{
e.printStackTrace();
}

ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();

for (int i = 0; i < sb.capacity(); i++) {
beatsShortList.add(sb.get(i));
}
}
}

最佳答案

感谢@Stephen C,问题是 while ((n = in1.read()) != -1) 所以我添加了一个缓冲区并将代码更改为以下内容和问题已修复,现在加载只需要几秒钟。感谢 Stephen C 的帮助以及 Ratul Shaker。

更新的代码:

                    byte[] buffer = new byte[0xFFFF];

while ((n = in1.read(buffer)) != -1)
{
bos.write(buffer, 0, n);
}

关于java - 加载字节数组花费太长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28654185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com