kopia lustrzana https://github.com/felHR85/UsbSerial
				
				
				
			added regex
							rodzic
							
								
									491bf0c029
								
							
						
					
					
						commit
						4a8e09dbf4
					
				| 
						 | 
					@ -2,33 +2,40 @@ package com.felhr.utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.UnsupportedEncodingException;
 | 
					import java.io.UnsupportedEncodingException;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.Arrays;
 | 
					import java.util.Iterator;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.regex.Matcher;
 | 
				
			||||||
 | 
					import java.util.regex.Pattern;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ProtocolBuffer {
 | 
					public class ProtocolBuffer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static final String BINARY = "binary";
 | 
					    public static final String BINARY = "binary.";
 | 
				
			||||||
    public static final String TEXT = "text";
 | 
					    public static final String TEXT = "text";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final String baseRegex1 = "(?<=";
 | 
					    /**
 | 
				
			||||||
 | 
					     * REGEX
 | 
				
			||||||
 | 
					     * .+?(?<=\\r\\n)
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final String baseRegex1 = ".+?(?<=";
 | 
				
			||||||
    private final String baseRegex2 = ")";
 | 
					    private final String baseRegex2 = ")";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private String mode;
 | 
					    private String mode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private String finalRegex;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private static final int DEFAULT_BUFFER_SIZE = 16 * 1024;
 | 
					    private static final int DEFAULT_BUFFER_SIZE = 16 * 1024;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private String trailChars;
 | 
					 | 
				
			||||||
    private String regex;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private byte[] rawBuffer;
 | 
					    private byte[] rawBuffer;
 | 
				
			||||||
 | 
					    private byte[] separator;
 | 
				
			||||||
    private StringBuilder stringBuffer;
 | 
					    private StringBuilder stringBuffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Iterator<String> iterator;
 | 
				
			||||||
    private List<String> commands = new ArrayList<>();
 | 
					    private List<String> commands = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Pattern wholeCommandPattern;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public ProtocolBuffer(String mode){
 | 
					    public ProtocolBuffer(String mode){
 | 
				
			||||||
        this.mode = mode;
 | 
					        this.mode = mode;
 | 
				
			||||||
 | 
					        this.iterator = commands.iterator();
 | 
				
			||||||
        if(mode.equals(BINARY)){
 | 
					        if(mode.equals(BINARY)){
 | 
				
			||||||
            rawBuffer = new byte[DEFAULT_BUFFER_SIZE];
 | 
					            rawBuffer = new byte[DEFAULT_BUFFER_SIZE];
 | 
				
			||||||
        }else{
 | 
					        }else{
 | 
				
			||||||
| 
						 | 
					@ -37,6 +44,8 @@ public class ProtocolBuffer {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public ProtocolBuffer(String mode, int bufferSize){
 | 
					    public ProtocolBuffer(String mode, int bufferSize){
 | 
				
			||||||
 | 
					        this.mode = mode;
 | 
				
			||||||
 | 
					        this.iterator = commands.iterator();
 | 
				
			||||||
        if(mode.equals(BINARY)){
 | 
					        if(mode.equals(BINARY)){
 | 
				
			||||||
            rawBuffer = new byte[bufferSize];
 | 
					            rawBuffer = new byte[bufferSize];
 | 
				
			||||||
        }else{
 | 
					        }else{
 | 
				
			||||||
| 
						 | 
					@ -45,11 +54,15 @@ public class ProtocolBuffer {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void setTrailChars(String trailChars){
 | 
					    public void setTrailChars(String trailChars){
 | 
				
			||||||
        finalRegex = baseRegex1 + adaptTrailChars(trailChars) + baseRegex2;
 | 
					        wholeCommandPattern = Pattern.compile(baseRegex1 + adaptTrailChars(trailChars) + baseRegex2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setSeparator(byte[] separator){
 | 
				
			||||||
 | 
					        this.separator = separator;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void setRegex(String regex){
 | 
					    public void setRegex(String regex){
 | 
				
			||||||
        finalRegex = regex;
 | 
					        wholeCommandPattern = Pattern.compile(regex);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void appendData(byte[] data){
 | 
					    public void appendData(byte[] data){
 | 
				
			||||||
| 
						 | 
					@ -57,15 +70,33 @@ public class ProtocolBuffer {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                String dataStr = new String(data, "UTF-8");
 | 
					                String dataStr = new String(data, "UTF-8");
 | 
				
			||||||
                stringBuffer.append(dataStr);
 | 
					                stringBuffer.append(dataStr);
 | 
				
			||||||
                String[] splitStr = stringBuffer.toString().split(finalRegex);
 | 
					
 | 
				
			||||||
                if(splitStr.length > 1){
 | 
					                Matcher matcher1 = wholeCommandPattern.matcher(stringBuffer.toString());
 | 
				
			||||||
                    commands.addAll(Arrays.asList(splitStr));
 | 
					                int groupCount = matcher1.groupCount();
 | 
				
			||||||
                    stringBuffer.setLength(0);
 | 
					
 | 
				
			||||||
 | 
					                for(int i=0;i<=groupCount-1;i++){
 | 
				
			||||||
 | 
					                    commands.add(matcher1.group(i));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                stringBuffer.replace(0, stringBuffer.length(), matcher1.replaceAll(""));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            } catch (UnsupportedEncodingException e) {
 | 
					            } catch (UnsupportedEncodingException e) {
 | 
				
			||||||
                e.printStackTrace();
 | 
					                e.printStackTrace();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        }else if(mode.equals(BINARY)){
 | 
				
			||||||
 | 
					            //TODO:!!
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public boolean hasMoreCommands(){
 | 
				
			||||||
 | 
					        return iterator.hasNext();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String nextCommand(){
 | 
				
			||||||
 | 
					        if(iterator.hasNext()){
 | 
				
			||||||
 | 
					            return iterator.next();
 | 
				
			||||||
 | 
					        }else{
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Ładowanie…
	
		Reference in New Issue