Android Framwork基础之FileInputStream文件数据流处理

纯纯是记录一下学习java framewrok

package com.android.fileinputstream;

import androidx.appcompat.app.AppCompatActivity;

import android.app.appsearch.AppSearchSchema;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    

    private static final String TAG = "jerry";

    //非纯数字字符串 16进制字符串转十进制
    public static long hexStringToAlgorism(String hex) {
    
    
        hex = hex.toUpperCase();
        int max = hex.length();
        long result = 0;
        for (int i = max; i > 0; i--) {
    
    
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
    
    
                algorism = c - '0';
            } else {
    
    
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }

    void DumpData(ArrayList<byte[]> byteList){
    
    
        Log.e(TAG, "the size = " + byteList.size());
        String  str = "";
        for(int i = 0; i < byteList.size(); i++){
    
    
                byte[] bytelist = byteList.get(i);
                str = "";
                for(int j = 0; j < 4; j++){
    
    
                    for(int k = 0; k < 16; k ++) {
    
    
                        str += Integer.toHexString(bytelist[j*16+k] & 0xFF);
                        str += " ";
                    }
                    str += "\n";
                }
                Log.e(TAG, "" + str);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener(){
    
    
            @Override
            public void onClick(View v){
    
    
                Log.e(TAG, "Click Down");
                try {
    
    
                    Log.e(TAG, "1.open file");
                    StringBuilder stringBuilder = new StringBuilder();
                    File file= new File("/vendor/firmware/RAM_BSL_00.08.08.39.txt");
                    FileInputStream fileInputStream = new FileInputStream(file);
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                    BufferedReader mBufferReader = new BufferedReader(inputStreamReader);
                    //String str = mBufferReader.readLine();//读取一行
                    String str;
                    while((str= mBufferReader.readLine()) != null){
    
    
                        String str1 = str.replaceAll(" +","");
                        String str2 = str1.replace("\n","");
                        Log.e(TAG, "" + str2);
                        stringBuilder.append(str2);
                    }
                    Log.e(TAG, "" + stringBuilder);
                    int endIndex = stringBuilder.indexOf("q");
                    int rest = 0;
                    int pkg_cnt = 0;
                    int ram_address = 0x002500;
                    String all_str = stringBuilder.substring(0, endIndex);
                    String[] splitData = all_str.split("@");
                    for(String tmp:splitData){
    
    
                        int len = tmp.length();
                        if(len > 0) {
    
    
                            int count = 0;
                            String rdata = tmp.substring(0,4);
                            count +=4;
                            //ram_address = Integer.parseInt(rdata);
                            ram_address =Integer.parseInt(rdata,16);//只能转纯数字字符串
                            String hexString = Integer.toHexString(ram_address);
                            Log.e(TAG, "the ram_address hex " + hexString + " string " + rdata + " int " + ram_address);
                            len = len -4;
                            if(len %48 == 0)
                                pkg_cnt = len / (48 * 2);
                            else
                                pkg_cnt = len / (48 * 2) + 1;
                            Log.e(TAG, "len " + len + " pkg_cnt " + pkg_cnt);

                            ArrayList<byte[]> byteList = new ArrayList<byte[]>();
                            for(int i=0; i< pkg_cnt; i++){
    
    
                                byte[] bytesArray = new byte[64];
                                bytesArray[0] = 0x3F;
                                bytesArray[1] = 0x21;
                                bytesArray[2] = 0x1B;
                                bytesArray[3] = (byte) (ram_address & 0xFF);
                                bytesArray[4] = (byte) ((ram_address >> 8) & 0xFF);
                                bytesArray[5] = (byte) ((ram_address >> 16) & 0xFF);
                                if(i != pkg_cnt - 1)
                                    rest = 48;
                                else
                                    rest = (len % 48)/2;

                                for(int j = 0; j < rest ; j++){
    
    
                                    if(i == pkg_cnt -1) {
    
    
                                        Log.e(TAG, " count " + count + " j " + j);
                                    }
                                    rdata = tmp.substring(count,count+2);
                                    int data = (int)hexStringToAlgorism(rdata);
//                                    if(i == 0) {
    
    
//                                        hexString = Integer.toHexString(data&0xFF);
//                                        Log.e(TAG, "the value hex " + hexString + " string " + rdata + " int " + data );
//                                    }
                                    count +=2;
                                    bytesArray[6+j] = (byte) data;
                                }
                                for(int k = rest+6; k < 64; k++){
    
    
                                    bytesArray[k] = (byte) 0xAC;
                                }
                                ram_address += 48;
                                byteList.add(bytesArray);
                            }
                            DumpData(byteList);
                        }
                    }
                }catch(Exception e) {
    
    
                    e.printStackTrace();
                    Log.e(TAG, "catch error");
                }
                Log.e(TAG, "Click Up");
            }



        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40405527/article/details/129969650