⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverdevice.java

📁 国外的j2me播放器软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (rs.getNumRecords() <= 0) {
                System.out.println(
                    "No records in RS. Creating well known containers");
                
                //
                // Create some well-known containers
                //
                ByteArrayOutputStream baos;
                DataOutputStream dos;
                byte[] objectSerialized;

                // Create root container
                // XXX root container has record id = 1 and id = 0
                root = new Container(0, -1, "root", false);
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(root.classType());
                root.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.addRecord(
                    objectSerialized, 
                    0, 
                    objectSerialized.length);
                dos.close();
                baos.close();
                System.out.println("Root container created");
                    
                // Create music container
                music = new StorageFolder(
                    rs.getNextRecordID(), 
                    root.id(), 
                    "Music", 
                    false, 
                    mediaRootConnection.directorySize(true));
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(music.classType());
                music.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.addRecord(objectSerialized, 0, objectSerialized.length);
                root.addContent(new Integer(music.id()));
                dos.close();
                baos.close();
                System.out.println("Music container created");
                
                // Create picture container
                pictures = new StorageFolder(
                    rs.getNextRecordID(), 
                    root.id(), 
                    "Pictures", 
                    false, 
                    // XXX Wrong size!
                    mediaRootConnection.directorySize(true));    
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(pictures.classType());
                pictures.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.addRecord(objectSerialized, 0, objectSerialized.length);
                root.addContent(new Integer(pictures.id()));
                dos.close();
                baos.close();
                
                System.out.println("Pictures container created");

                // Create picture container
                presentations = new StorageFolder(
                    rs.getNextRecordID(), 
                    root.id(), 
                    "Presentations", 
                    false, 
                    // XXX Wrong size!
                    mediaRootConnection.directorySize(true));    
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(presentations.classType());
                presentations.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.addRecord(objectSerialized, 0, objectSerialized.length);
                root.addContent(new Integer(presentations.id()));
                dos.close();
                baos.close();
                
                System.out.println("Presentations container created");
                
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(root.classType());
                root.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.setRecord(
                    1,
                    objectSerialized, 
                    0, 
                    objectSerialized.length);
                dos.close();
                baos.close();
                System.out.println("Root updated");
            } else {
                ByteArrayInputStream bais = new ByteArrayInputStream(
                    rs.getRecord(1));
                DataInputStream dis = new DataInputStream(bais);
                dis.readUTF();
                root = new Container(dis);

                bais = new ByteArrayInputStream(rs.getRecord(2));
                dis = new DataInputStream(bais);
                dis.readUTF(); 
                music = new StorageFolder(dis);

                bais = new ByteArrayInputStream(rs.getRecord(3));
                dis = new DataInputStream(bais);
                dis.readUTF();
                pictures = new StorageFolder(dis);
            }
        }

        public void run() {
            try{
                ByteArrayOutputStream baos;
                DataOutputStream dos;
                byte[] objectSerialized;

                for (Enumeration fileList = mediaRootConnection.list(); 
                     fileList.hasMoreElements();) 
                {
                    try {
                        String fileName=(String)fileList.nextElement();
                        
                        FileConnection file = (FileConnection)Connector.open(
                            "file:///" + mediaRoot + fileName);
                        
                        //
                        // Don't go into directories currently
                        //
                        if (file.isDirectory()) {
                            continue;
                        }
                        
                        if (rs.enumerateRecords(
                            new IsFileIndexedFilter(file.getURL()),
                            null,
                            false).numRecords() > 0)
                        {                            
                            //System.out.println(
                            //    "File already indexed. Skipping");
                            continue;
                        }
                        
                        baos =new ByteArrayOutputStream();
                        dos = new DataOutputStream(baos);

                        //System.out.println(
                        //    "Tentative ID=" + rs.getNextRecordID() + 
                        //    "; fileSize= " + file.fileSize() + 
                        //    "; url= " + file.getURL());
                        
                        String title = fileName.substring(
                            0,
                            fileName.lastIndexOf('.'));
                        
                        if (fileName.endsWith(".mp3")) {
                            MusicTrack mt = new MusicTrack(
                                rs.getNextRecordID(), 
                                music.id(), 
                                title,
                                false,
                                file.getURL());
                            mt.setResource(
                                ServerDevice.getContentLocationPrefix() + 
                                mt.id() + ".mp3");
                            dos.writeUTF(mt.classType());
                            mt.serialize(dos);
                            music.addContent(new Integer(mt.id()));
                        } else if (
                            fileName.endsWith(".jpg") || 
                            fileName.endsWith(".jpeg"))
                        {
                            Photo p = new Photo(
                                rs.getNextRecordID(),
                                pictures.id(),
                                title,
                                false,
                                file.getURL());
                            p.setResource(
                                ServerDevice.getContentLocationPrefix() + 
                                p.id() + ".jpg");
                            dos.writeUTF(p.classType());
                            p.serialize(dos);
                            pictures.addContent(new Integer(p.id()));
                        } else if (
                            fileName.endsWith(".ppt")) 
                        {
                            PresentationItem p = new PresentationItem(
                                    rs.getNextRecordID(),
                                    presentations.id(),
                                    title,
                                    false,
                                    file.getURL());
                            p.setResource(
                                    ServerDevice.getContentLocationPrefix() +
                                    p.id() + ".ppt");
                            dos.writeUTF(p.classType());
                            p.serialize(dos);
                            presentations.addContent(new Integer(p.id()));
                        } else {
                            baos = null;
                            dos = null;
                        }

                        if (baos != null && dos != null) {
                            objectSerialized = baos.toByteArray();
                            rs.addRecord(
                                objectSerialized, 
                                0, 
                                objectSerialized.length);
                            baos.close();
                            dos.close();
                        }
                    } catch(IOException ae) {
                        LOG.warn("IOException while indexing file");
                        LOG.warn(ae);
                        ae.printStackTrace();
                    }
                }    
                
                // XXX Must re-add the containers due to their content has 
                //     been changed. Quite stupid way to do this, should be 
                //     changed in the future. Problem is that a container 
                //     must know its childCount property (for metadata) when 
                //     serializing as XML, and I assume it is expensive to do 
                //     this through a RecordFilter looking for parentId. 
                //     Especially the cost of binding the Container class more 
                //     strictly to the specific record store.
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(root.classType());
                root.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.setRecord(
                    1, 
                    objectSerialized, 
                    0, 
                    objectSerialized.length);

                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(music.classType());
                music.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.setRecord(
                    music.id(), 
                    objectSerialized, 
                    0, 
                    objectSerialized.length);

                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(pictures.classType());
                pictures.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.setRecord(
                    pictures.id(), 
                    objectSerialized, 
                    0, 
                    objectSerialized.length);
                
                baos = new ByteArrayOutputStream();
                dos = new DataOutputStream(baos);
                dos.writeUTF(presentations.classType());
                presentations.serialize(dos);
                objectSerialized = baos.toByteArray();
                rs.setRecord(
                    presentations.id(), 
                    objectSerialized, 
                    0, 
                    objectSerialized.length);                
            } catch (IOException ioe) {
                LOG.fatal("IOException while indexing files");
                ioe.printStackTrace();
            } catch(RecordStoreException rse){
                LOG.fatal("RSE while indexing files");
                LOG.fatal(rse);
                rse.printStackTrace();
            } catch (NullPointerException npe) {
                LOG.fatal(npe);
                System.out.println("Error while indexing files");
                System.out.println(npe);
                npe.printStackTrace();
            } /*finally {
                if (rs != null) {
                    try{
                        rs.closeRecordStore();
                    } catch(Exception e){
                        System.out.println(
                            "Error closing the RecordStore for file index.");
                        e.printStackTrace();
                    }
                }
            } */            
        }         
    }

    private final class IsFileIndexedFilter implements RecordFilter {
        private String url;
        
        public IsFileIndexedFilter(String fileUrl) {
            this.url= fileUrl;
        }
            
        public boolean matches(byte[] candidate) {
            String candidateUrl = null;
            
            if (candidate == null || candidate.length == 0) {
                return false;
            }

            try {                
                ByteArrayInputStream bais = new ByteArrayInputStream(
                    candidate);
                DataInputStream dis = new DataInputStream(bais);

                //
                // Must skip until object.item::localUrl is found 
                //
                // ClassType
                // DidlObject:
                //      dos.writeInt(id);
                //      dos.writeInt(parentId);
                //      dos.writeBoolean(restricted);
                //      dos.writeUTF(title);
                //      dos.writeUTF(creator);
                //      dos.writeUTF(res);
                //      dos.writeUTF(writeStatus);
                // Item:
                //      dout.writeUTF(refID);
                //      dout.writeUTF(localUrl);
                //
                dis.skipBytes(dis.readUnsignedShort() + 4 + 4 + 1);
                dis.skipBytes(dis.readUnsignedShort()); // title
                dis.skipBytes(dis.readUnsignedShort()); // creator
                dis.skipBytes(dis.readUnsignedShort()); // res
                dis.skipBytes(dis.readUnsignedShort()); // writeStatus
                dis.skipBytes(dis.readUnsignedShort()); // refID
            
                candidateUrl = dis.readUTF();
                //System.out.println(candidateUrl + " == " + url);
            } catch (java.io.IOException ioe) {
                System.out.println("ioexception while filtering records");
                ioe.printStackTrace();
            }

            return url.equals(candidateUrl);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -