2011-11-23 112 views
1

我有一個低級別的緩存機制,它從服務器接收一個json數組並將其緩存在一個文件中。刪除流中的開頭和結尾字符

實際的緩存機制只是將大的流流保存到文件中,而不會意識到它是json。因此,當我想通過聚合流到另一個文件我結束了這樣的追加流到現有文件緩存:

[{"id":3144,"created_at":"1322064201"}][{"id":3144,"created_at":"1322064201"}] 

那裏顯然是我的願望是這樣的:

[{"id":3144,"created_at":"1322064201"},{"id":3144,"created_at":"1322064201"}] 

這樣做的最有效的方法是什麼?

我已經調查FilterReader但看到我所知,所有我真正需要做的是刪除現有的緩存和新的內容[第一個字符的最後一個字符]並添加,我認爲有可能是一個更好的辦法比檢查這些大流中的每個字符。

對於上下文我的代碼確實是這樣的:

... input stream passed with new content 

    File newCache = new File("JamesBluntHatersClub") 
    FileOutputStream tempFileOutputStream = new FileOutputStream(newCache); 
    FileInputStream fileInputStream = new FileInputStream(existingCache); 
    copyStream(fileInputStream, tempFileOutputStream); 
    copyStream(inputStream, tempFileOutputStream); 

    ... clean up 

UPDATE:

已經實施了FilterReader它一次像這樣檢查字符之一:

@Override 
public int read() throws IOException { 
    int content = super.read(); 
    // replace open square brackets with comma 
    switch (content) { 
     case SQUARE_BRACKETS_OPEN: 
      return super.read(); 
     case SQUARE_BRACKETS_CLOSE: 
      return super.read(); 
     default: 
      return content; 
    } 
} 

的處理時間慢得令人難以接受,所以我正在尋找另一種選擇。我想使用的文件的大小來確定該文件的大小和刪除尾方括號這樣

回答

0

這種方法奏效了

/** 
* Copys the input streams in order to the output stream and retains json array 
* format 
* 
* @param inputStreamA 
* @param inputStreamB 
* @param outputStream 
* @throws IOException 
*/ 
private void copyStreamsToOutput(InputStream inputStreamA, InputStream inputStreamB, 
     FileOutputStream outputStream) throws IOException { 
    copyStream(inputStreamA, outputStream); 
    // truncate file to remove trailing ']' 
    outputStream.getChannel().truncate(outputStream.getChannel().size() - 1); 
    // add comma between json objects 
    outputStream.write(COMMA); 
    // skip '[' 
    inputStreamB.skip(1); 
    // and copy rest of streamas normal 
    copyStream(inputStreamB, outputStream); 
} 

會非常有興趣到這裏,如果這是不好的做法,我猜測可能存在編碼問題。

UPDATE

/** 
* Copys the input streams in order to output stream and retains json array 
* format 
* 
* @param inputStreamA 
* @param inputStreamB 
* @param outputStream 
* @throws IOException 
*/ 
private void copyStreamsToOutput(InputStream inputStreamA, InputStream inputStreamB, 
     FileOutputStream outputStream) throws IOException { 
    copyStream(inputStreamA, outputStream); 
    long channelSize = outputStream.getChannel().size(); 
    // truncate file to remove trailing ']' 
    outputStream.getChannel().truncate(channelSize - 1); 
    // check to see if array was empty (2 = []) 
    if (channelSize > 2) { 
     // add comma between json objects 
     outputStream.write(COMMA); 
    } 
    // skip '[' 
    inputStreamB.skip(1); 
    // and copy rest of streams normal 
    copyStream(inputStreamB, outputStream); 
    long newChannelSize = outputStream.getChannel().size(); 
    // check if we haven't just added a empty array 
    if(newChannelSize - channelSize < 2){ 
     // if so truncate to remove comma 
     outputStream.getChannel().truncate(channelSize - 1); 
     outputStream.write(CLOSE_SQUARE_BRACKET); 
    } 
} 

新增要麼流

+0

在處理一個空的JSON數組的能力似乎你需要開始一個非空緩存([])。您可以將最後一個字符替換爲,。 –

+0

我不確定我是否完全理解,你是在暗示我先寫'[]'到outputstream,然後用','替換上一個']'? – zode64

+0

正如您已經建議您的方法不適用於每種編碼。在這種情況下,我向代碼添加一些斷言,並提到Javadoc中該方法的侷限性。添加一些單元測試後,這是質量代碼,不是不好的練習。 您的第一個解決方案的處理時間是不可接受的慢?你是否將FilterReader的參數包裝在BufferedReader中以提高性能?只是問... – rwitzel

相關問題