Package org.jruby.util

Examples of org.jruby.util.ByteList


        return ((JRubyConstructor)ctor).runtime.newFloat(((Double)SafeConstructorImpl.constructYamlFloat(ctor,node)).doubleValue());
    }
    public static Object constructYamlBinary(final Constructor ctor, final Node node) {
        Object b = SafeConstructorImpl.constructYamlBinary(ctor,node);
        if(b instanceof byte[]) {
            return RubyString.newString(((JRubyConstructor)ctor).runtime, new ByteList((byte[])b,false));
        } else {
            return ((JRubyConstructor)ctor).runtime.newString((String)b);
        }
    }
View Full Code Here


        }

        LibCSocket.sockaddr_un sockaddr = new LibCSocket.sockaddr_un();
        sockaddr.setFamily(RubySocket.AF_UNIX);

        ByteList path = _path.convertToString().getByteList();
        fpath = path.toString();

        if(sockaddr.sun_path.length <= path.realSize) {
            throw runtime.newArgumentError("too long unix socket path (max: " + (sockaddr.sun_path.length-1) + "bytes)");
        }
View Full Code Here

        if(slen < buflen) {
            buflen = slen;
        }
        byte[] outp = new byte[buflen];
        str.get(outp);
        RubyString _str = context.getRuntime().newString(new ByteList(outp, 0, buflen, false));

        return context.getRuntime().newArrayNoCopy(new IRubyObject[]{_str, unixaddr(context.getRuntime(), buf, alen)});
    }
View Full Code Here

        if (separatorString == null) {
            return readall();
        }

        final ByteList separator = (separatorString == PARAGRAPH_DELIMETER) ?
            PARAGRAPH_SEPARATOR : separatorString;

        descriptor.checkOpen();
       
        if (feof()) {
            return null;
        }
       
        int c = read();
       
        if (c == -1) {
            return null;
        }
       
        // unread back
        buffer.position(buffer.position() - 1);

        ByteList buf = new ByteList(40);
       
        byte first = separator.bytes[separator.begin];

        LineLoop : while (true) {
            ReadLoop: while (true) {
                byte[] bytes = buffer.array();
                int offset = buffer.position();
                int max = buffer.limit();
               
                // iterate over remainder of buffer until we find a match
                for (int i = offset; i < max; i++) {
                    c = bytes[i];
                    if (c == first) {
                        // terminate and advance buffer when we find our char
                        buf.append(bytes, offset, i - offset);
                        if (i >= max) {
                            buffer.clear();
                        } else {
                            buffer.position(i + 1);
                        }
                        break ReadLoop;
                    }
                }
               
                // no match, append remainder of buffer and continue with next block
                buf.append(bytes, offset, buffer.remaining());
                int read = refillBuffer();
                if (read == -1) break LineLoop;
            }
           
            // found a match above, check if remaining separator characters match, appending as we go
            for (int i = 0; i < separator.realSize; i++) {
                if (c == -1) {
                    break LineLoop;
                } else if (c != separator.bytes[separator.begin + i]) {
                    buf.append(c);
                    continue LineLoop;
                }
                buf.append(c);
                if (i < separator.realSize - 1) {
                    c = read();
                }
            }
            break;
View Full Code Here

            if (left <= 0) {
                eof = true;
                return null;
            }
            left += ungotc != -1 ? 1 : 0;
            ByteList result = new ByteList((int) left);
            ByteBuffer buf = ByteBuffer.wrap(result.unsafeBytes(),
                    result.begin(), (int) left);
            if (ungotc != -1) {
                buf.put((byte) ungotc);
                ungotc = -1;
            }
            while (buf.hasRemaining()) {
                int n = ((ReadableByteChannel) descriptor.getChannel()).read(buf);
                if (n <= 0) {
                    break;
                }
            }
            eof = true;
            result.length(buf.position());
            return result;
        } else if (descriptor.isNull()) {
            return new ByteList(0);
        } else {
            checkReadable();

            ByteList byteList = new ByteList();
            ByteList read = fread(BUFSIZE);
           
            if (read == null) {
                eof = true;
                return byteList;
            }
View Full Code Here

    public synchronized ByteList read(int number) throws IOException, BadDescriptorException {
        checkReadable();
        ensureReadNonBuffered();
       
        ByteList byteList = new ByteList(number);
       
        // TODO this should entry into error handling somewhere
        int bytesRead = descriptor.read(number, byteList);
       
        if (bytesRead == -1) {
View Full Code Here

    private ByteList bufferedRead(int number) throws IOException, BadDescriptorException {
        checkReadable();
        ensureRead();
       
        ByteList result = new ByteList(0);
       
        int len = -1;
        if (buffer.hasRemaining()) { // already have some bytes buffered
            len = (number <= buffer.remaining()) ? number : buffer.remaining();
            result.append(buffer, len);
        }
        boolean done = false;
        //
        // Avoid double-copying for reads that are larger than the buffer size
        //
        while ((number - result.length()) >= BUFSIZE) {
            //
            // limit each iteration to a max of BULK_READ_SIZE to avoid over-size allocations
            //
            int bytesToRead = Math.min(BULK_READ_SIZE, number - result.length());
            int n = descriptor.read(bytesToRead, result);
            if (n == -1) {
                eof = true;
                done = true;
                break;
            } else if (n == 0) {
                done = true;
                break;
            }
        }
       
        //
        // Complete the request by filling the read buffer first
        //
        while (!done && result.length() != number) {
            int read = refillBuffer();
           
            if (read == -1) {
                eof = true;
                break;
            } else if (read == 0) {
                break;
            }
           
            // append what we read into our buffer and allow the loop to continue
            int desired = number - result.length();
            len = (desired < read) ? desired : read;
            result.append(buffer, len);
        }
       
        if (result.length() == 0 && number != 0) {
            if (eof) {
                throw new EOFException();
            }
        }
        return result;
View Full Code Here

        try {
            if (number == 0) {
                if (eof) {
                    return null;
                } else {
                    return new ByteList(0);
                }
            }

            if (ungotc >= 0) {
                ByteList buf2 = bufferedRead(number - 1);
                buf2.prepend((byte)ungotc);
                ungotc = -1;
                return buf2;
            }

            return bufferedRead(number);
View Full Code Here

        }
        // make sure that the ungotc is not forgotten
        if (ungotc >= 0) {
            number--;
            if (number == 0 || !buffer.hasRemaining()) {
                ByteList result = new ByteList(new byte[] {(byte)ungotc}, false);
                ungotc = -1;
                return result;
            }
        }

        if (buffer.hasRemaining()) {
            // already have some bytes buffered, just return those

            ByteList result = bufferedRead(Math.min(buffer.remaining(), number));

            if (ungotc >= 0) {
                result.prepend((byte)ungotc);
                ungotc = -1;
            }
            return result;
        } else {
            // otherwise, we try an unbuffered read to get whatever's available
View Full Code Here

            RubyArray args = (RubyArray)runtime.getGlobalVariables().get("$*");
            if (args.getLength() == 0) {
                if (!startedProcessing) {
                    currentFile = runtime.getGlobalVariables().get("$stdin");
                    ((RubyString) runtime.getGlobalVariables().get("$FILENAME")).setValue(new ByteList(new byte[]{'-'}));
                    currentLineNumber = 0;
                    startedProcessing = true;
                    return true;
                } else {
                    finishedProcessing = true;
                    return false;
                }
            }

            IRubyObject arg = args.shift(context);
            RubyString filename = (RubyString)((RubyObject)arg).to_s();
            ByteList filenameBytes = filename.getByteList();
            ((RubyString) runtime.getGlobalVariables().get("$FILENAME")).setValue(filenameBytes);

            if (filenameBytes.length() == 1 && filenameBytes.get(0) == '-') {
                currentFile = runtime.getGlobalVariables().get("$stdin");
            } else {
                currentFile = RubyFile.open(context, runtime.getFile(),
                        new IRubyObject[] {filename.strDup(context.getRuntime())}, Block.NULL_BLOCK);
            }
View Full Code Here

TOP

Related Classes of org.jruby.util.ByteList

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.