首页 > 解决方案 > Parsing DAT file using bouncy castle

问题描述

The dat file is being extracted. There is a lot of data in it. However, some data is published without problems, while others indicate errors.

Here is my code

public void Decode(File filePath) {
        byte[] blockBuffer = new byte[0x800];
        ASN1InputStream bIn;
        ASN1Primitive obj;
        try {
            FileInputStream fileInput = new FileInputStream(filePath);
            notFound = false;
            String checkFileSql = "SELECT count(*) AS cnt FROM table_name WHERE filename=?";
            PreparedStatement checkStmt = conn.prepareStatement(checkFileSql);
            checkStmt.setString(1, filePath.getName());
            ResultSet rs = checkStmt.executeQuery();
            if (rs.next()) {
                if (rs.getInt("cnt") == 0) {
                    isExist = false;
                    String insertSql = "INSERT..."
                    stmt = conn.prepareStatement(insertSql);
                    while (fileInput.available() > 0) {
                        if (fileInput.read(blockBuffer) > 0) {
                            bIn = new ASN1InputStream(blockBuffer);
                            boolean endOfBlock = false;
                            while (bIn.available() > 4 && !endOfBlock) {
                                if (bIn.markSupported()) {
                                    bIn.mark(4);
                                    if (bIn.read() == 0xff && bIn.read() == 0xff && bIn.read() == 0xff
                                            && bIn.read() == 0xff) {
                                        endOfBlock = true;
                                    }
                                    bIn.reset();
                                    if (!endOfBlock) {
                                        obj = (ASN1Primitive) bIn.readObject();
                                        Record cr = new Record();
                                        cr.parseObject(obj);
                                        if (cr.cdrType != null) {
                                            if (cr.cdrType == cr.pgwCDR || cr.cdrType == cr.sgwCDR) {
                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }

                    int[] rslt = stmt.executeBatch();

                    for (int i = 0; i < rslt.length; i++) {
                        inserted += rslt[i];
                    }

                    conn.commit();

                    stmt.close();
                    fileInput.close();
                } else {
                    isExist = true;
                }
            }
            rs.close();
            checkStmt.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            exceptionStr = e.getMessage();
            e.printStackTrace();
        }
    }

Error message

java.io.EOFException: DEF length 224 object truncated by 90 at org.bouncycastle.asn1.DefiniteLengthInputStream.read(Unknown Source) at org.bouncycastle.util.io.Streams.readFully(Unknown Source) at org.bouncycastle.util.io.Streams.readFully(Unknown Source) at org.bouncycastle.asn1.DefiniteLengthInputStream.toByteArray(Unknown Source) at org.bouncycastle.asn1.ASN1StreamParser.readTaggedObject(Unknown Source) at org.bouncycastle.asn1.BERTaggedObjectParser.getLoadedObject(Unknown Source) at org.bouncycastle.asn1.ASN1StreamParser.readVector(Unknown Source) at org.bouncycastle.asn1.ASN1StreamParser.readTaggedObject(Unknown Source) at org.bouncycastle.asn1.ASN1InputStream.buildObject(Unknown Source) at org.bouncycastle.asn1.ASN1InputStream.readObject(Unknown Source)

Here is parsing data code

public void parseObject(ASN1Primitive obj) {
        if (obj instanceof DERTaggedObject) {
         DERTaggedObject.getInstance(obj);
            DERTaggedObject cdrObj = (DERTaggedObject) DERTaggedObject.getInstance(obj);
            cdrType = cdrObj.getTagNo();
            if (cdrObj.getObject() instanceof DLSequence) {
                DLSequence callModule = (DLSequence) DLSequence.getInstance(cdrObj.getObject());
                for (int i = 0; i < callModule.size(); i++) {
                    if (callModule.getObjectAt(i) instanceof DERTaggedObject) {
                        DERTaggedObject eventModule = (DERTaggedObject) DERTaggedObject
                                .getInstance(callModule.getObjectAt(i));
                        int tag = eventModule.getTagNo();
                        distValues(cdrType, tag, eventModule);
                    }
                }
            }
        }
    }

The line indicating the error message

obj = (ASN1Primitive) bIn.readObject();

            

标签: javaexceptionbouncycastleasn.1decoder

解决方案


推荐阅读