Reads a tag and sets the bits to a new tag at same time for max
speedification
13816
long readTagAndSet(long bucketIndex, int posInBucket, long newTag) {
		long tagStartIdx = getTagOffset(bucketIndex, posInBucket);
		long tag = 0;
		long tagEndIdx = tagStartIdx + bitsPerTag;
		int tagPos = 0;
		for (long i = tagStartIdx; i < tagEndIdx; i++) {
			if ((newTag & (1L << tagPos)) != 0) {
				if (memBlock.getAndSet(i)) {
					tag |= 1 << tagPos;
				}
			} else {
				if (memBlock.getAndClear(i)) {
					tag |= 1 << tagPos;
				}
			}
			tagPos++;
		}
		return tag;
	}
200910
public static Tag readTag(DataInput in) throws IOException {
        int id = in.readUnsignedByte();
        if(id == 0) {
            return null;
        }

        String name = in.readUTF();
        Tag tag;

        try {
            tag = TagRegistry.createInstance(id, name);
        } catch(TagCreateException e) {
            throw new IOException("Failed to create tag.", e);
        }

        tag.read(in);
        return tag;
    }
329345
protected void readTag(int tag)
        throws IOException
    {
        int length = (is.read() << 8) + is.read();
        is.skip(length);
    }
34877
def process_boolean_array(self, tag):
        """"""
        array_size = tag.get_array_size()
        tag.set_address(self.normal_register.get_array(array_size))
        if self.is_sixteen_bit:
            # each boolean address needs 1/16 byte
            self.normal_register.move_to_next_address((array_size / 16) + 1)
            return
        # each boolean address needs 1/8 byte
        self.normal_register.move_to_next_address((array_size / 8) + 1)
94782
@SuppressWarnings ({"unchecked", "rawtypes"})
    private Tag readTagPayload(TagType type, String name, int depth) throws IOException {
        switch (type) {
            case TAG_END:
                if (depth == 0) {
                    throw new IOException("TAG_End found without a TAG_Compound/TAG_List tag preceding it.");
                } else {
                    return new EndTag();
                }

            case TAG_BYTE:
                return new ByteTag(name, is.readByte());

            case TAG_SHORT:
                return new ShortTag(name, is.readShort());

            case TAG_INT:
                return new IntTag(name, is.readInt());

            case TAG_LONG:
                return new LongTag(name, is.readLong());

            case TAG_FLOAT:
                return new FloatTag(name, is.readFloat());

            case TAG_DOUBLE:
                return new DoubleTag(name, is.readDouble());

            case TAG_BYTE_ARRAY:
                int length = is.readInt();
                byte[] bytes = new byte[length];
                is.readFully(bytes);
                return new ByteArrayTag(name, bytes);

            case TAG_STRING:
                length = is.readShort();
                bytes = new byte[length];
                is.readFully(bytes);
                return new StringTag(name, new String(bytes, NBTConstants.CHARSET.name()));

            case TAG_LIST:
                TagType childType = TagType.getById(is.readByte());
                length = is.readInt();

                Class<? extends Tag> clazz = childType.getTagClass();
                List<Tag> tagList = new ArrayList<Tag>(length);
                for (int i = 0; i < length; i++) {
                    Tag tag = readTagPayload(childType, "", depth + 1);
                    if (tag instanceof EndTag) {
                        throw new IOException("TAG_End not permitted in a list.");
                    } else if (!clazz.isInstance(tag)) {
                        throw new IOException("Mixed tag types within a list.");
                    }
                    tagList.add(tag);
                }

                return new ListTag(name, clazz, tagList);

            case TAG_COMPOUND:
                CompoundMap compoundTagList = new CompoundMap();
                while (true) {
                    Tag tag = readTag(depth + 1);
                    if (tag instanceof EndTag) {
                        break;
                    } else {
                        compoundTagList.put(tag);
                    }
                }

                return new CompoundTag(name, compoundTagList);

            case TAG_INT_ARRAY:
                length = is.readInt();
                int[] ints = new int[length];
                for (int i = 0; i < length; i++) {
                    ints[i] = is.readInt();
                }
                return new IntArrayTag(name, ints);

            case TAG_SHORT_ARRAY:
                length = is.readInt();
                short[] shorts = new short[length];
                for (int i = 0; i < length; i++) {
                    shorts[i] = is.readShort();
                }
                return new ShortArrayTag(name, shorts);

            default:
                throw new IOException("Invalid tag type: " + type + ".");
        }
    }
1076773
def __parse_tag(self, tag, count):
        """"""

        fileobj = cBytesIO(tag)

        for i in xrange(count):
            tag_data = fileobj.read(8)
            # someone writes wrong item counts
            if not tag_data:
                break
            if len(tag_data) != 8:
                raise error
            size = cdata.uint32_le(tag_data[:4])
            flags = cdata.uint32_le(tag_data[4:8])

            # Bits 1 and 2 bits are flags, 0-3
            # Bit 0 is read/write flag, ignored
            kind = (flags & 6) >> 1
            if kind == 3:
                raise APEBadItemError("value type must be 0, 1, or 2")

            key = value = fileobj.read(1)
            if not key:
                raise APEBadItemError
            while key[-1:] != b'\x00' and value:
                value = fileobj.read(1)
                if not value:
                    raise APEBadItemError
                key += value
            if key[-1:] == b"\x00":
                key = key[:-1]

            if PY3:
                try:
                    key = key.decode("ascii")
                except UnicodeError as err:
                    reraise(APEBadItemError, err, sys.exc_info()[2])
            value = fileobj.read(size)
            if len(value) != size:
                raise APEBadItemError

            value = _get_value_type(kind)._new(value)

            self[key] = value
778490
private Tag readTag(int depth) throws IOException {
        int typeId = is.readByte() & 0xFF;
        TagType type = TagType.getById(typeId);

        String name;
        if (type != TagType.TAG_END) {
            int nameLength = is.readShort() & 0xFFFF;
            byte[] nameBytes = new byte[nameLength];
            is.readFully(nameBytes);
            name = new String(nameBytes, NBTConstants.CHARSET.name());
        } else {
            name = "";
        }

        return readTagPayload(type, name, depth);
    }
371289
private void writeTagPayload(Tag<?> tag) throws IOException {
        switch (tag.getType()) {
            case TAG_END:
                writeEndTagPayload((EndTag) tag);
                break;

            case TAG_BYTE:
                writeByteTagPayload((ByteTag) tag);
                break;

            case TAG_SHORT:
                writeShortTagPayload((ShortTag) tag);
                break;

            case TAG_INT:
                writeIntTagPayload((IntTag) tag);
                break;

            case TAG_LONG:
                writeLongTagPayload((LongTag) tag);
                break;

            case TAG_FLOAT:
                writeFloatTagPayload((FloatTag) tag);
                break;

            case TAG_DOUBLE:
                writeDoubleTagPayload((DoubleTag) tag);
                break;

            case TAG_BYTE_ARRAY:
                writeByteArrayTagPayload((ByteArrayTag) tag);
                break;

            case TAG_STRING:
                writeStringTagPayload((StringTag) tag);
                break;

            case TAG_LIST:
                writeListTagPayload((ListTag<?>) tag);
                break;

            case TAG_COMPOUND:
                writeCompoundTagPayload((CompoundTag) tag);
                break;

            case TAG_INT_ARRAY:
                writeIntArrayTagPayload((IntArrayTag) tag);
                break;

            case TAG_SHORT_ARRAY:
                writeShortArrayTagPayload((ShortArrayTag) tag);
                break;

            default:
                throw new IOException("Invalid tag type: " + tag.getType() + ".");
        }
    }
923630
def chunks(f):
        """"""
        while 1:
            try:
                length = struct.unpack(b"!I", f.read(4))[0]
                tag = f.read(4)
                data = f.read(length)
                crc = struct.unpack(b"!I", f.read(4))[0]
            except struct.error:
                return
            if zlib.crc32(tag + data) & 0xFFFFFFFF != crc:
                raise IOError('Checksum fail')
            yield tag, data
294761
public int readBytes(byte[] buffer, int offset, int length)
        throws IOException
    {
        int readLength = 0;

        if (_chunkLength == END_OF_DATA) {
            _chunkLength = 0;
            return -1;
        }
        else if (_chunkLength == 0) {
            int tag = read();

            switch (tag) {
                case 'N':
                    return -1;

                case 'B':
                case 'b':
                    _isLastChunk = tag == 'B';
                    _chunkLength = (read() << 8) + read();
                    break;

                default:
                    throw new IOException("expected 'B' at " + (char) tag);
            }
        }

        while (length > 0) {
            if (_chunkLength > 0) {
                buffer[offset++] = (byte) read();
                _chunkLength--;
                length--;
                readLength++;
            }
            else if (_isLastChunk) {
                if (readLength == 0)
                    return -1;
                else {
                    _chunkLength = END_OF_DATA;
                    return readLength;
                }
            }
            else {
                int tag = read();

                switch (tag) {
                    case 'B':
                    case 'b':
                        _isLastChunk = tag == 'B';
                        _chunkLength = (read() << 8) + read();
                        break;

                    default:
                        throw new IOException("expected 'B' at " + (char) tag);
                }
            }
        }

        if (readLength == 0)
            return -1;
        else if (_chunkLength > 0 || !_isLastChunk)
            return readLength;
        else {
            _chunkLength = END_OF_DATA;
            return readLength;
        }
    }
725547
def set_tags(self, md5, tags):
        """"""
        if isinstance(tags, str):
            tags = [tags]
        tag_set = set(tags)
        self.data_store.store_work_results({'tags': list(tag_set)}, 'tags', md5)
1079464
def _process_tags(self):
        """"""
        tags = []

        while True:
            tag_bf = unpack_ui16(self._src)
            tag_type = tag_bf >> 6   # upper 10 bits
            if tag_type == 0:
                # the end
                break
            tag_len = tag_bf & 0x3f  # last 6 bits
            if tag_len == 0x3f:
                # the length is the next four bytes!
                tag_len = unpack_ui32(self._src)

            try:
                tag_name = TAG_NAMES[tag_type]
            except KeyError:
                # malformed SWF, create and unknown object with malformed tag
                tag_payload = self._src.read(tag_len)
                _dict = {
                    '__str__': _repr,
                    '__repr__': _repr,
                    'name': 'UnspecifiedObject(tag={!r})'.format(tag_type),
                }
                tag = type("UnknownObject", (SWFObject,), _dict)()
                tag.raw_payload = tag_payload
                tags.append(tag)
                continue

            try:
                tag_meth = getattr(self, "_handle_tag_" + tag_name.lower())
            except AttributeError:
                if self.unknown_alert:
                    raise ValueError("Unknown tag: " + repr(tag_name))

                tag_payload = self._src.read(tag_len)
                _dict = {'__str__': _repr, '__repr__': _repr, 'name': tag_name}
                tag = type("UnknownObject", (SWFObject,), _dict)()
                tag.raw_payload = tag_payload
                tags.append(tag)
                continue

            # we know the tag type, and have the handler, let's process it
            prev_pos = self._src.tell()
            self._src.guard = tag_len
            try:
                with ReadQuantityController(self._src, tag_len):
                    tag = tag_meth()
                assert tag is not None, tag_name
            except ValueError:
                # an attempt to read too much happened; create a failing
                # object with the raw payload
                self._src.guard = None
                self._src.seek(prev_pos)
                tag_payload = self._src.read(tag_len)
                _dict = {'__str__': _repr, '__repr__': _repr, 'name': tag_name}
                tag = type("FailingObject", (SWFObject,), _dict)()
                tag.raw_payload = tag_payload
            tags.append(tag)
        return tags
458429
private int readNext(byte[] dest, int offset, int len)
            throws IOException
    {
        int readBytes = 0;
        while (readBytes < len) {
            int ret = in.read(dest, readBytes + offset, len - readBytes);
            if (ret == -1) {
                finishedReading = true;
                return readBytes;
            }
            readBytes += ret;
        }
        return readBytes;
    }
577631
@Override
    public ITag readTag() {
        ITag tag = null;
        if (log.isTraceEnabled()) {
            log.trace("Read tag - prevFrameSize {} audio: {} video: {}", new Object[] { prevFrameSize, audioCount, videoCount });
        }
        // ensure there are frames before proceeding
        if (!frames.isEmpty()) {
            try {
                lock.acquire();
                //log.debug("Read tag");
                //empty-out the pre-streaming tags first
                if (!firstTags.isEmpty()) {
                    //log.debug("Returning pre-tag");
                    // Return first tags before media data
                    return firstTags.removeFirst();
                }
                //get the current frame
                MP4Frame frame = frames.get(currentFrame);
                if (frame != null) {
                    log.debug("Playback #{} {}", currentFrame, frame);
                    int sampleSize = frame.getSize();
                    int time = (int) Math.round(frame.getTime() * 1000.0);
                    //log.debug("Read tag - dst: {} base: {} time: {}", new Object[]{frameTs, baseTs, time});
                    long samplePos = frame.getOffset();
                    //log.debug("Read tag - samplePos {}", samplePos);
                    //determine frame type and packet body padding
                    byte type = frame.getType();
                    //assume video type
                    int pad = 5;
                    if (type == TYPE_AUDIO) {
                        pad = 2;
                    }
                    //create a byte buffer of the size of the sample
                    ByteBuffer data = ByteBuffer.allocate(sampleSize + pad);
                    try {
                        //prefix is different for keyframes
                        if (type == TYPE_VIDEO) {
                            if (frame.isKeyFrame()) {
                                //log.debug("Writing keyframe prefix");
                                data.put(PREFIX_VIDEO_KEYFRAME);
                            } else {
                                //log.debug("Writing interframe prefix");
                                data.put(PREFIX_VIDEO_FRAME);
                            }
                            // match the sample with its ctts / mdhd adjustment time
                            int timeOffset = prevVideoTS != -1 ? time - prevVideoTS : 0;
                            data.put((byte) ((timeOffset >>> 16) & 0xff));
                            data.put((byte) ((timeOffset >>> 8) & 0xff));
                            data.put((byte) (timeOffset & 0xff));
                            if (log.isTraceEnabled()) {
                                byte[] prefix = new byte[5];
                                int p = data.position();
                                data.position(0);
                                data.get(prefix);
                                data.position(p);
                                log.trace("{}", prefix);
                            }
                            // track video frame count
                            videoCount++;
                            prevVideoTS = time;
                        } else {
                            //log.debug("Writing audio prefix");
                            data.put(PREFIX_AUDIO_FRAME);
                            // track audio frame count
                            audioCount++;
                        }
                        // do we need to add the mdat offset to the sample position?
                        dataSource.position(samplePos);
                        // read from the channel
                        dataSource.read(data);
                    } catch (IOException e) {
                        log.error("Error on channel position / read", e);
                    }
                    // chunk the data
                    IoBuffer payload = IoBuffer.wrap(data.array());
                    // create the tag
                    tag = new Tag(type, time, payload.limit(), payload, prevFrameSize);
                    //log.debug("Read tag - type: {} body size: {}", (type == TYPE_AUDIO ? "Audio" : "Video"), tag.getBodySize());
                    // increment the frame number
                    currentFrame++;
                    // set the frame / tag size
                    prevFrameSize = tag.getBodySize();
                }
            } catch (InterruptedException e) {
                log.warn("Exception acquiring lock", e);
            } finally {
                lock.release();
            }
        } else {
            log.warn("No frames are available for the requested item");
        }
        //log.debug("Tag: {}", tag);
        return tag;
    }
1075452
def readbits(self, bits):
        
        
        if bits == 0:
            return 0
        
        # fast byte-aligned path
        if bits % 8 == 0 and self._bits_pending == 0:
            return self._read_bytes_aligned(bits // 8)
        
        out = 0
        masks = self._masks
        
        def transfer_bits(x, y, n, t):
            """
            transfers t bits from the top of y_n to the bottom of x.
            then returns x and the remaining bits in y
            """
            if n == t:
                # taking all
                return (x << t) | y, 0
            
            mask = masks[t]           # (1 << t) - 1
            remainmask = masks[n - t] # (1 << n - t) - 1
            taken = ((y >> n - t) & mask)
            return (x << t) | taken, y & remainmask
        
        while bits > 0:
            if self._bits_pending > 0:
                assert self._partial_byte is not None
                take = min(self._bits_pending, bits)
                out, self._partial_byte = transfer_bits(out, self._partial_byte, self._bits_pending, take)
                
                if take == self._bits_pending:
                    # we took them all
                    self._partial_byte = None
                self._bits_pending -= take
                bits -= take
                continue
            
            r = self.f.read(1)
            if r == b'':
                raise EOFError
            self._partial_byte = ord(r)
            self._bits_pending = 8
        
        return out
806363
void setBits(BitSet table) {
    for (int c = Character.MAX_VALUE; c >= Character.MIN_VALUE; c--) {
      if (matches((char) c)) {
        table.set(c);
      }
    }
  }
1256573
def read(cls, read, has_name=True):
        
        name = cls._read_utf8(read) if has_name else None

        if cls is TAG_Compound:
            # A TAG_Compound is almost identical to Python's native dict()
            # object, or a Java HashMap.
            final = {}
            while True:
                # Find the type of each tag in a compound in turn.
                tag = read('b', 1)[0]
                if tag == 0:
                    # A tag of 0 means we've reached TAG_End, used to terminate
                    # a TAG_Compound.
                    break
                # We read in each tag in turn, using its name as the key in
                # the dict (Since a compound cannot have repeating names,
                # this works fine).
                tmp = _tags[tag].read(read)
                final[tmp.name] = tmp
            return cls(final, name=name)
        elif cls is TAG_List:
            # A TAG_List is a very simple homogeneous array, similar to
            # Python's native list() object, but restricted to a single type.
            tag_type, length = read('bi', 5)
            tag_read = _tags[tag_type].read
            return cls(
                _tags[tag_type],
                [tag_read(read, has_name=False) for x in range(0, length)],
                name=name
            )
        elif cls is TAG_String:
            # A simple length-prefixed UTF-8 string.
            value = cls._read_utf8(read)
            return cls(value, name=name)
        elif cls is TAG_Byte_Array:
            # A simple array of (signed) bytes.
            length = read('i', 4)[0]
            return cls(read('{0}b'.format(length), length), name=name)
        elif cls is TAG_Int_Array:
            # A simple array of (signed) 4-byte integers.
            length = read('i', 4)[0]
            return cls(read('{0}i'.format(length), length * 4), name=name)
        elif cls is TAG_Long_Array:
            # A simple array of (signed) 8-byte longs.
            length = read('i', 4)[0]
            return cls(read('{0}q'.format(length), length * 8), name=name)
        elif cls is TAG_Byte:
            # A single (signed) byte.
            return cls(read('b', 1)[0], name=name)
        elif cls is TAG_Short:
            # A single (signed) short.
            return cls(read('h', 2)[0], name=name)
        elif cls is TAG_Int:
            # A signed (signed) 4-byte int.
            return cls(read('i', 4)[0], name=name)
        elif cls is TAG_Long:
            # A single (signed) 8-byte long.
            return cls(read('q', 8)[0], name=name)
        elif cls is TAG_Float:
            # A single single-precision floating point value.
            return cls(read('f', 4)[0], name=name)
        elif cls is TAG_Double:
            # A single double-precision floating point value.
            return cls(read('d', 8)[0], name=name)
        elif cls is TAG_End:
            # A End of Compound Tag
            return cls(read('2b', 2)[0], name=name)