---
title: "用socket实现的文件服务器(3)"
author: "hongsoft"
date: "2005-09-28T20:41:00Z"
tags: []
slug: "socket-3"
draft: false
lastmod: "2005-09-28T20:41:00Z"
id: 287449
url: "https://hongsoft.iteye.com/blog/287449"
---

//主题帖cache规则：读则加入，否则不需要加入  
    private static Cache topicCache = new Cache(300\*1000\*1000);  
    //回帖cache  
    private static Cache replyCache = new Cache(300\*1000\*1000);

    /\**  
     * 取主题贴，首先应该从cache中取  
     *  
     * @param forumId  
     * @param topicId  
     * @return  
     \*/  
    public String getTopicContent(int forumId, int topicId)  
    {  
        //如果cache中有，就从cache中取  
        Object content=topicCache.get(forumId+"\_"+topicId);  
        if(content!=null)  
        {  
            return content.toString();  
        }  
        // 如果cache中没有，就从文件中读  
        try  
        {  
            BufferedReader in = new BufferedReader(new FileReader(getTopicFile(  
                    forumId, topicId)));  
            String s = new String();  
            String s2 = new String();  
            while ((s = in.readLine()) != null)  
            {  
                s2 += s;  
            }  
            in.close();  
            //加入cache  
            topicCache.add(forumId+"\_"+topicId,s2);  
            return s2;  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return "";  
    }

    /\**  
     * 取回贴，首先应该从cache中取  
     *  
     * @param forumId  
     * @param topicId  
     * @return  
     \*/  
    public List getReplyList(int forumId, int topicId)  
    {  
        //如果cache中有，就从cache中取  
        Object replyList=replyCache.get(forumId+"\_"+topicId);  
        if(replyList!=null)  
        {  
            return (ArrayList)replyList;  
        }  
        // 如果cache中没有，就从文件中读  
        List list = new ArrayList();// 回贴的文件记录的list，不需要分解。分解工作在client来完成  
        try  
        {            
            RandomAccessFile in = new RandomAccessFile(getReplyFile(forumId, topicId),"rw");  
            String s = new String();  
            while ((s = in.readLine()) != null)  
            {  
                list.add(s);  
            }  
            in.close();  
            //加入到cache中  
            replyCache.add(forumId+"\_"+topicId,list);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return list;  
    }

    /\**  
     * 发帖  
     *  
     * @param forumId  
     * @param topicId  
     * @return  
     \*/  
    public void addTopic(int forumId, int topicId, String content)  
    {  
        try  
        {  
            String filePath=getTopicPath(forumId, topicId);  
            // 需要判断文件的目录是否存在  
            File path=new File(filePath);  
            if(!path.exists())  
            {  
               path.mkdirs();  
            }  
            //写入文件  
            PrintWriter out = new PrintWriter(new FileWriter(getTopicFile(forumId,topicId)));  
            out.write(content);  
            out.close();  
        }  
        catch (Exception e)  
        {  
            throw new RuntimeException(e);  
        }  
    }  
    /\**  
     * 续贴  
     * @param forumId  
     * @param topicId  
     * @param content  
     \*/  
    public void resumeTopic(int forumId, int topicId, String content)  
    {  
        try  
        {  
            String filePath=getTopicFile(forumId, topicId);  
            // 需要判断文件的目录是否存在  
            File path=new File(filePath);  
            if(!path.exists())  
            {  
               path.mkdirs();  
            }  
            //写入文件  
            content=getTopicContent(forumId,topicId)+content;  
            PrintWriter out = new PrintWriter(new FileWriter(filePath));  
            out.write(content);  
            out.close();  
            //如果cache中有，则需要更新  
            Object cacheContent=topicCache.get(forumId+"\_"+topicId);  
            if(cacheContent!=null)  
            {  
                topicCache.add(forumId+"\_"+topicId,content);  
            }  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
    /\**  
     * 回贴，如果没有回过贴，要新建文件；如果回过，要续加回贴  
     * @param forumId  
     * @param topicId  
     * @return  
     \*/  
    public void addReply(int forumId, int topicId,String content,String qq,String replyTime)  
    {  
        int replyCount=0;//回贴数s  
        try  
        {  
            String filePath=getReplyFile(forumId, topicId);  
            String replyCountPath=getReplyCountFile(forumId,topicId);  
            // 需要判断文件是否存在  
            File file=new File(filePath);             
            if(!file.exists())//以前没有回过贴，回贴数为0+1=1  
            {  
                (new File(getReplyPath(forumId,topicId))).mkdirs();  
                (new File(getReplyCountPath(forumId,topicId))).mkdirs();  
                replyCount++;  
            }  
            else//以前回过贴，从文件中读出回贴数，并修改为+1  
            {                
                DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(replyCountPath)));                 
                replyCount=in.readInt()+1;              
                in.close();                 
            }  
            //修改回贴数  
            DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(replyCountPath)));  
            out.writeInt(replyCount);  
            out.close();  
            //写入文件             
            RandomAccessFile rf=new RandomAccessFile(filePath,"rw");  
            rf.seek(rf.length());  
            String str=new Integer(replyCount)+"\\0x1e"+qq+"\\0x1e"+replyTime+"\\0x1e"+content+"\\n";  
            rf.write(str.getBytes());             
            rf.close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }     
    }  
    /\**  
     * 删主题贴和它的回贴  
     * @param forumId  
     * @param topicId  
     * @param content  
     \*/  
    public void deleteTopic(int forumId, int topicId)  
    {  
        try  
        {        
            // 需要判断文件是否存在  
            File path=new File(getTopicFile(forumId, topicId));  
            if(path.exists())  
            {  
                path.delete();  
            }  
            //如果cache中有，则需要删除  
            Object cacheContent=topicCache.get(forumId+"\_"+topicId);  
            if(cacheContent!=null)  
            {  
                topicCache.remove(forumId+"\_"+topicId);  
            }  
            //删除回贴  
            File reply=new File(getReplyFile(forumId,topicId));  
            if(reply.exists())  
            {  
                reply.delete();  
            }  
            File replyCount=new File(getReplyCountFile(forumId,topicId));  
            if(replyCount.exists())  
            {  
                replyCount.delete();  
            }  
            //如果cache中有，需要删除  
            Object replyList=replyCache.get(forumId+"\_"+topicId);  
            if(replyList!=null)  
            {  
                replyCache.remove(forumId+"\_"+topicId);  
            }  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
    /\**  
     * 删某个回贴  
     * @param forumId  
     * @param topicId  
     * @param content  
     \*/  
    public void deleteReply(int forumId, int topicId,int replyId)  
    {  
        try  
        {        
            //把满足条件的记录写到bak文件中  
            String filePath=getReplyFile(forumId, topicId);          
            RandomAccessFile rf=new RandomAccessFile(filePath,"rw");  
            RandomAccessFile rfBak=new RandomAccessFile(filePath+".bak","rw");  
            String s;  
            while((s=rf.readLine())!=null)  
            {  
                String\[] strs=s.split("\\0x1e");  
                if((new Integer(replyId)).toString().equals(strs\[0]))  
                {  
                    //do nothing  
                }  
                else  
                {  
                    rfBak.write((s+"\\n").getBytes());  
                }                     
            }  
            rfBak.close();  
            rf.close();  
            //原文件删除  
            File file=new File(filePath);  
            file.delete();  
            //新文件改名  
            File file2=new File(filePath+".bak");  
            file2.renameTo(new File(filePath));  
            //如果cache中有，需要删除  
            Object replyList=replyCache.get(forumId+"\_"+topicId);  
            if(replyList!=null)  
            {  
                replyCache.remove(forumId+"\_"+topicId);  
            }  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }