Package ca.odell.glazedlists.impl.io

Examples of ca.odell.glazedlists.impl.io.Bufferlo


        assert(offset != -1);
        assert(size() != -1);

        // prepare to read
        FileChannel fileChannel = persistentMap.getFileChannel();
        Bufferlo chunkAsBytes = new Bufferlo();
        DataInputStream dataIn = new DataInputStream(chunkAsBytes.getInputStream());

        // read the whole chunk
        int bytesRequired = size();
        int read = chunkAsBytes.readFromChannel(fileChannel.position(offset), bytesRequired);
        if(read < bytesRequired) throw new IOException("Expected " + bytesRequired + " but found " + read + " bytes");

        // skip the chunk header
        dataIn.readInt(); // on/off
        dataIn.readInt(); // size to use
        dataIn.readInt(); // size 1
        dataIn.readInt(); // size 2

        // read the data
        sequenceId = dataIn.readInt();
        keyBytesLength = dataIn.readInt();
        valueBytesLength = dataIn.readInt();
        keyBytes = chunkAsBytes.consume(keyBytesLength);

        // skip any excess
        chunkAsBytes.clear();

        // process the read data
        key = persistentMap.getKeyCoder().decode(keyBytes.getInputStream());
    }
View Full Code Here


        assert(size() != -1);
        assert(valueBytesLength != -1);

        // prepare to read
        FileChannel fileChannel = persistentMap.getFileChannel();
        Bufferlo valueBytes = new Bufferlo();
        int valueLocation = offset;

        // adjust the value location: header
        valueLocation += 4; // on/off
        valueLocation += 4; // sizeToUse
        valueLocation += 4; // size one
        valueLocation += 4; // size two

        // adjust the value location: body
        valueLocation += 4; // sequence ID
        valueLocation += 4; // key size
        valueLocation += 4; // value size
        valueLocation += keyBytesLength; // key

        // read
        int read = valueBytes.readFromChannel(fileChannel.position(valueLocation), valueBytesLength);
        if(read < valueBytesLength) throw new IOException("Expected " + valueBytesLength + " but found " + read + " bytes");

        // done
        return valueBytes;
    }
View Full Code Here

     * doesn't exist.
     */
    private void readHeader() throws IOException {
        try {
            // process the file header
            Bufferlo fileHeader = new Bufferlo();
            fileHeader.readFromChannel(persistentMap.getFileChannel(), 8);
            fileHeader.consume("GLAZED\n\n");
               
        } catch(ParseException e) {
            // the file header is broken, bail
            throw new IOException("The file cannot be read because it is not of the expected type");
        }
View Full Code Here

    /**
     * Creates the PersistentMap file.
     */
    private void createFile() throws IOException {
        // write the file header
        Bufferlo fileHeader = new Bufferlo();
        fileHeader.write("GLAZED\n\n");
        persistentMap.getFileChannel().position(0);
        fileHeader.writeToChannel(persistentMap.getFileChannel());
        logger.info("Successfully created file");
    }
View Full Code Here

    /** {@inheritDoc} */
    public void listChanged(ListEvent<E> listChanges) {
        // notify resource listeners
        try {
            ListEvent<E> listChangesCopy = listChanges.copy();
            Bufferlo listChangesBytes = ListEventToBytes.toBytes(listChangesCopy, byteCoder);
            for(int r = 0; r < resourceListeners.size(); r++) {
                ResourceListener listener = resourceListeners.get(r);
                listener.resourceUpdated(privateInterfaces, listChangesBytes.duplicate());
            }
        } catch(IOException e) {
            throw new IllegalStateException(e.getMessage());
        }
       
View Full Code Here

   
    /**
     * Get a binary snapshot of this resource in its current state.
     */
    public Bufferlo toSnapshot() {
        Bufferlo result = new Bufferlo();
        result.write(value);
        return result;
    }
View Full Code Here

     * will be considered local if the host specified matches the host in the uri.
     * Otherwise the uri will be considered remote.
     */
    public static ResourceUri localOrRemote(String uri, String localHost, int localPort) {
        try {
            Bufferlo parser = new Bufferlo();
            parser.write(uri);
           
            parser.consume("glazedlists\\:\\/\\/");
            String host = parser.readUntil("\\:");
            String portString = parser.readUntil("\\/");
            int port = Integer.parseInt(portString);
            String path = "/" + parser.toString();
           
            boolean local = (localHost.equals(host)) && (localPort == port);
            return new ResourceUri(host, port, path, local);
        } catch(ParseException e) {
            throw new IllegalStateException(e.getMessage());
View Full Code Here

    private void remoteSubscribe(ResourceConnection subscriber, PeerBlock block) {
        // we're accepting connections
        if(resourceStatus.isConnected()) {
            // save the update id and a snapshot
            int updateId = -1;
            Bufferlo snapshot = null;
            resource.getReadWriteLock().writeLock().lock();
            try {
                updateId = resourceUpdateId;
                snapshot = resource.toSnapshot();
            } finally {
View Full Code Here

        int lengthAfterHeaders = bytes.length();
        int headersLength = lengthBeforeHeaders - lengthAfterHeaders;
       
        // load the data
        int payloadLength = blockSizeWithHeaders - headersLength;
        Bufferlo payload = bytes.consume(payloadLength);
        bytes.consume("\\r\\n");
       
        // parse the headers
        String resourceUriString = (String)headers.get(RESOURCE_URI);
        ResourceUri resourceUri = ResourceUri.localOrRemote(resourceUriString, localHost, localPort);
View Full Code Here

    /**
     * Get the bytes for this block.
     */
    public Bufferlo toBytes(String localHost, int localPort) {
        // the writer with no size info
        Bufferlo writer = new Bufferlo();
       
        // populate the map of headers
        Map headers = new TreeMap();
        if(resourceUri != null) headers.put(RESOURCE_URI, resourceUri.toString(localHost, localPort));
        if(sessionId != -1) headers.put(SESSION_ID, new Integer(sessionId));
        if(action != null) headers.put(ACTION, action);
        if(updateId != -1) headers.put(UPDATE_ID, new Integer(updateId));
       
        // write the header values
        for(Iterator i = headers.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry mapEntry = (Map.Entry)i.next();
            writer.write(mapEntry.getKey().toString());
            writer.write(": ");
            writer.write(mapEntry.getValue().toString());
            writer.write("\r\n");
        }
        writer.write("\r\n");

        // write the payload
        if(payload != null) {
            writer.append(payload.duplicate());
        }
       
        // wrap the size
        Bufferlo writerWithSize = new Bufferlo();
        writerWithSize.write("" + writer.length());
        writerWithSize.write("\r\n");
        writerWithSize.append(writer);
        writerWithSize.write("\r\n");
       
        // all done
        return writerWithSize;
    }
View Full Code Here

TOP

Related Classes of ca.odell.glazedlists.impl.io.Bufferlo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.