본문

class SMSProcessor : 문자메시지 80byte단위 처리

요즘 문자메시지 관련 프로그램을 만들고 있는데, 노트북 메인보드가 나갔지만 AS문제때문에 어쩔수없이 고치지 못하고 공용컴퓨터에서 틈나는대로 작업하는 중이다. .NET은 그나마 윈도우 업데이트하면서 프레임워크가 자동으로 깔리니까 좋은데 jre는 일일히 다시 깔아줘야 해서 여간 귀찮은게 아니다. 그래도 jre 설치 속도가 더 빠르고, USB에 이클립스를 넣고 다니니까 편해서 그것에 위안을 삼는다. 아무튼 요즘은 블로그에 글쓰는게 어려운 상황이다.

문자메시지는 80바이트 단위로 보낼 수 밖에 없기 때문에 80바이트가 넘어가면 80바이트 단위로 끊어서 여러번 보내야 한다. 예전에 c#으로 만들어 뒀었는데 노트북 하드에 저장되어 있어 꺼내기 귀찮았다.(다음에 이것도 올려야지) 그래서 네이버에서 "자바 80바이트"라고 치니까 이건 뭐 자바스크립트만 나오네... 그래서 그냥 새로 만들기로 했다. ....ㄷㄷㄷ

일말의 고심도 없이 코드가 머리에서 스트리밍 되었고, 막쓰긴 좋은거 같아 올려본다. 이건 뭐 워낙 간단하니 주석이 필요가 없다;)



import java.util.ArrayList;

public class SMSProcessor {
    ArrayList<String> strings;
    public SMSProcessor(String org){
        strings=this.stringToSMSList(org);
    }
   
    public int getSMSNum(){
        return this.strings.size();
    }

    public ArrayList<String> getSMSArray(){
        return this.strings;
    }
   
    public static ArrayList<String> stringToSMSList(String org){
        if(org==null || org.equals("")) return null;
        byte[] bytes=org.getBytes();
        String newString;
        ArrayList<String> arr=new ArrayList<String>();
        int start=0;
        do{
                newString=byteToString80(bytes, start);
                start+=80;
                arr.add(newString);
        }while(start<bytes.length);
        return arr;
    }

    public static String byteToString80(byte[] bytes, int start){
        byte[] newByte=new byte[80];
        int i=0;
        int limit=bytes.length-start;
        while(i<limit && i<80){
            newByte[i]=bytes[i+start];
            ++i;
        }
        String ret=new String(newByte);
        return ret;
    }
}

댓글

Holic Spirit :: Tistory Edition

design by tokiidesu. powerd by kakao.