English 中文(简体)
字符串阵列在拆分字符串后丢弃 Arra Index out out ofboundsExpendation 时丢弃矩形
原标题:String array throws ArrayIndexOutOfBoundsException after splitting a String

我试图开发一个基于网络服务网络的在线游戏,

游戏机械学现在运作良好, 我真的很想继续我的下一个步骤 我的发展计划, 但现在我被困在一个简单的编码/解码功能 我创建这个功能是为了将我的数据 存储到一个字符串里。

我采取的步骤没有那么复杂。 在运行期间, 我的服务器创建了游戏对象的“ 图像国家 ” 。 每个图像国家都包含许多图层, 其中包含在特定时间等地点必须绘制的细节 。

我尝试了ObjectInput/ExputStream, 加上GZip-Compression和ByteInputStreams, 但我找不到任何GWT执行前两个方案的情况,

我的目标是将这些细节编码成一个字符串,压缩在服务器上,将压缩数据发送到客户端,解压缩并解码到客户端,这样客户端就可以“读”它。为了这样做,我创建了一个编码和一个解码功能:

编码器 :

public static String encodeImageStateContainer(HashMap<String,HashMap<Long,ImageState>> imageStateContainer){
    StringBuilder mainStringBuilder = new StringBuilder();

    Iterator<Entry<String,HashMap<Long,ImageState>>> imageStateContainerIterator = imageStateContainer.entrySet().iterator();
    while(imageStateContainerIterator.hasNext()){
        Entry<String,HashMap<Long,ImageState>> tempImageStateManagerMetadata = imageStateContainerIterator.next();

        HashMap<Long,ImageState> tempImageStateManager = tempImageStateManagerMetadata.getValue();

        if(tempImageStateManager.size() > 0){

            if(mainStringBuilder.length() > 0){
                mainStringBuilder.append( } );  //Divisor between Image State Managers
            }

            mainStringBuilder.append(tempImageStateManagerMetadata.getKey());
            mainStringBuilder.append( | );  //Divisor between Name and following Data

            StringBuilder subOneStringBuilder = new StringBuilder();            

            Iterator<Entry<Long,ImageState>> tempImageStateManagerIterator = tempImageStateManager.entrySet().iterator();
            while(tempImageStateManagerIterator.hasNext()){
                Entry<Long,ImageState> tempImageStateMetaData = tempImageStateManagerIterator.next();

                if(subOneStringBuilder.length() > 0){
                    subOneStringBuilder.append( ) );    //Divisor between Image Objects
                }

                subOneStringBuilder.append(tempImageStateMetaData.getKey());
                subOneStringBuilder.append( - );    //Divisor between Object Id and Image State Data

                StringBuilder subTwoStringBuilder = new StringBuilder();
                ImageState tempImageState = tempImageStateMetaData.getValue();
                subOneStringBuilder.append(tempImageState.getFirstFrameId());   //1. => First Frame Id
                subOneStringBuilder.append( ; );    //Divisor between Image State data types
                subOneStringBuilder.append(tempImageState.getFramesPerLayer()); //2. => Total Frame Count
                subOneStringBuilder.append( ; );
                subOneStringBuilder.append(tempImageState.getMinumimScaleFactor()); //3. => Minimum Scale Factor
                subOneStringBuilder.append( ; );

                ImageStateLayer[] tempImageStateLayers = tempImageState.getImageStateLayers();
                for(int layerId = 0; layerId < tempImageStateLayers.length; ++layerId){

                    if(subTwoStringBuilder.length() > 0){
                        subTwoStringBuilder.append( / );    //Divisor between Image State Layers
                    }

                    //Frame Arrays
                    String[] imageNativePath = tempImageStateLayers[layerId].getImageNativePath();
                    short[] offsetX = tempImageStateLayers[layerId].getOffsetX();
                    short[] offsetY = tempImageStateLayers[layerId].getOffsetY();
                    short[] orientation = tempImageStateLayers[layerId].getOrientation();

                    //Finalization Arrays
                    byte[] imagePathChange = tempImageStateLayers[layerId].getImagePathChange();
                    byte[] offsetXChange = tempImageStateLayers[layerId].getOffsetXChange();
                    byte[] offsetYChange = tempImageStateLayers[layerId].getOffsetYChange();
                    byte[] orientationChange = tempImageStateLayers[layerId].getOrientationChange();


                    //Image Path Data
                    StringBuilder subThreeStrignBuilder = new StringBuilder();
                    for(int imagePathId = 0; imagePathId < imageNativePath.length; ++imagePathId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append( # );  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(imageNativePath[imagePathId]);
                        subThreeStrignBuilder.append( , );  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(imagePathChange[imagePathId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append( [ );    //Divisor between Frame Data Types

                    //OffsetX Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int offsetXId = 0; offsetXId < offsetX.length; ++offsetXId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append( # );  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(offsetX[offsetXId]);
                        subThreeStrignBuilder.append( , );  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(offsetXChange[offsetXId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append( [ );    //Divisor between Frame Data Types

                    //OffsetY Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int offsetYId = 0; offsetYId < offsetY.length; ++offsetYId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append( # );  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(offsetY[offsetYId]);
                        subThreeStrignBuilder.append( , );  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(offsetYChange[offsetYId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append( [ );    //Divisor between Frame Data Types

                    //Orientation Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int orientationId = 0; orientationId < orientation.length; ++orientationId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append( # );  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(orientation[orientationId]);
                        subThreeStrignBuilder.append( , );  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(orientationChange[orientationId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());

                }
                subOneStringBuilder.append(subTwoStringBuilder.toString());             
            }
            mainStringBuilder.append(subOneStringBuilder.toString());
        }
    }

    return mainStringBuilder.toString();
}

解码器:

public static HashMap<String,HashMap<Long,ImageState>> decodeImageStateContainer(String data){      

    String[] imageStateManagerArray = data.split("\}");
    HashMap<String,HashMap<Long,ImageState>> imageStateManagerContainer = new HashMap<String,HashMap<Long,ImageState>>(imageStateManagerArray.length);

    for(int managerId = 0; managerId < imageStateManagerArray.length; ++managerId){
        String[] tempImageStateData = imageStateManagerArray[managerId].split("\|");

        HashMap<Long,ImageState> tempImageStateManager = new HashMap<Long,ImageState>();
        imageStateManagerContainer.put(tempImageStateData[0], tempImageStateManager);

        String[] tempImageStateManagerObjects = tempImageStateData[1].split("\)");

        for(int objectId = 0; objectId < tempImageStateManagerObjects.length; ++objectId){
            String[] tempImageObjectData = tempImageStateManagerObjects[objectId].split("\-");

            long imageObjectId = Long.parseLong(tempImageObjectData[0]);
            String[] imageStateMetaData = tempImageObjectData[1].split("\;");              
            ImageState tempImageState = new ImageState(Integer.parseInt(imageStateMetaData[1]), Integer.parseInt(imageStateMetaData[0]), Integer.parseInt(imageStateMetaData[2]));
            tempImageStateManager.put(imageObjectId, tempImageState);

            String[] tempImageStateLayerMetaData = imageStateMetaData[3].split("\/");
            ImageStateLayer[] tempImageStateLayers = new ImageStateLayer[tempImageStateLayerMetaData.length];

            for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
                String[] layerElements = tempImageStateLayerMetaData[layerId].split("\[");     

                String[] imageNativePathDetails = layerElements[0].split("\#");
                String[] offsetXDetails = layerElements[1].split("\#");
                String[] offsetYDetails = layerElements[2].split("\#");
                **String[] orientationDetails = layerElements[3].split("\#");**

                //Image Path Data
                String[] imageNativePaths = new String[imageNativePathDetails.length];
                byte[] imagePathChange = new byte[imageNativePathDetails.length];
                for(int id = 0; id < imageNativePathDetails.length; ++id){                      
                    String[] imagePathDetailElements = imageNativePathDetails[id].split("\,");
                    imageNativePaths[id] = imagePathDetailElements[0];
                    imagePathChange[id] = Byte.parseByte(imagePathDetailElements[1]);
                }

                //OffsetX Data
                short[] offsetX = new short[offsetXDetails.length];
                byte[] offsetXChange = new byte[offsetXDetails.length];
                for(int id = 0; id < offsetXDetails.length; ++id){
                    String[] offsetXDetailElements = offsetXDetails[id].split("\,");
                    offsetX[id] = Short.parseShort(offsetXDetailElements[0]);
                    offsetXChange[id] = Byte.parseByte(offsetXDetailElements[1]);
                }

                //OffsetY Data
                short[] offsetY = new short[offsetYDetails.length];
                byte[] offsetYChange = new byte[offsetYDetails.length];
                for(int id = 0; id < offsetYDetails.length; ++id){
                    String[] offsetYDetailElements = offsetYDetails[id].split("\,");
                    offsetY[id] = Short.parseShort(offsetYDetailElements[0]);
                    offsetYChange[id] = Byte.parseByte(offsetYDetailElements[1]);
                }

                //Orientation Data
                short[] orientation = new short[orientationDetails.length];
                byte[] orientationChange = new byte[orientationDetails.length];
                for(int id = 0; id < orientationDetails.length; ++id){
                    String[] orientationDetailElements = orientationDetails[id].split("\,");
                    orientation[id] = Short.parseShort(orientationDetailElements[0]);
                    orientationChange[id] = Byte.parseByte(orientationDetailElements[1]);
                }

                //Create the Layer and Add it to the Array
                tempImageStateLayers[layerId] = new ImageStateLayer(imageNativePaths,new short[][]{offsetX,offsetY,orientation}, new byte[][]{imagePathChange,offsetXChange,offsetYChange,orientationChange});

            }

            //Connect the Reference to the layers with the Image State
            tempImageState.setImageStateLayers(tempImageStateLayers);

        }           

    }       

    return imageStateManagerContainer;
}

现在,我的问题是:如果我在服务器上编码它,然后直接解码它, 它需要大约三个编码周期 直到我得到一个索引外的外的边界例外 在解码器的这个特定位置, 最后一行是错误的线条, 根据堆叠跟踪, 错误似乎在这条线上 :

for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
                String[] layerElements = tempImageStateLayerMetaData[layerId].split("\[");     

                String[] imageNativePathDetails = layerElements[0].split("\#");
                String[] offsetXDetails = layerElements[1].split("\#");
                String[] offsetYDetails = layerElements[2].split("\#");
                String[] orientationDetails = layerElements[3].split("\#");

我不会说我是专家,但在测试编码器之后,我绝对可以说它产生的字符串总是有效的,我得到这个错误的字符串也是完整的。我的解码功能一定有问题,但我不知道它会是什么。

正如我所说,前三个周期的编码和编码正确,编码数据没有发生巨大变化。 编码字符串不可能导致字符串数组小于4个元素。 基本上,只要我理解我的代码,这个错误就不可能存在。

我认为这可能是某种记忆分配问题, 防止弦被正确分开, 但这只是一个概念 一个无知的程序员。

任何帮助都是感激不尽的!

最佳回答

我找到了解决我问题的办法:

在编码时,我在将数字类型打印到我的字符串中。在选择我的“选手”符号时,我并没有考虑负值,结果在数字考虑之前就出现了负值,这导致我试图分开时,我的字符串无节制地分裂。我用另一个字符替换了负值,现在一切都正常了!

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签